Does anyone have an easy implementation of a debou...
# compose
z
Does anyone have an easy implementation of a debounce button in compose?
s
Not sure if it's the best implementation out there, but we've been using this one quite successfully
Copy code
fun Modifier.delayedClickable(
  delay: Long = 2_000,
  enabled: Boolean = true,
  onClickLabel: String? = null,
  role: Role? = null,
  onClick: () -> Unit
) = composed {
  var lastClickTime by remember { mutableStateOf(0L) }

  Modifier.clickable(
    enabled = enabled,
    onClickLabel = onClickLabel,
    role = role,
    onClick = {
      if (SystemClock.elapsedRealtime() - lastClickTime >= delay) {
        lastClickTime = SystemClock.elapsedRealtime()
        onClick()
      }
    },
  )
}
🙌 1
🔥 2
w
I will also note that we made our lastClickTime a static object so people couldn't click two separate objects at the same time. It does create a risk of nested throttling, but we also made it log when it happens to make identifying throttled errors easier.
🙏 1
z
Amazing! thank you very much!!