```var timer by remember { mutableStateOf(time) } ...
# compose
i
Copy code
var timer by remember { mutableStateOf(time) }
    LaunchedEffect(key1 = timer) {
        if (timer > 0) {
            delay(1000L)
            timer -= 1000L
        }
    }
Is this is best way to do timer per seconds?
f
use CountdownTimer instead
s
Depends how accurate you want it to be, I’d expect CountdownTimer to be much better in general, but would love to be corrected here. Doing this in coroutines I’d expect in the suspension points of delay etc you’d be consistently missing some milliseconds here and there. With that said, for a compose implementation, I’d say you should do this instead of having both a mutableState and a LaunchedEffect.
Copy code
val timer by produceState(initialValue = 0L) {
  while (isActive) {
    delay(1.seconds)
    value += 1.seconds.inWholeMilliseconds
  }
}
This is what produceState exists for, gives you initial value, and a coroutine scope in which you can do stuff in.