How to call a function every 5 second in Jetpack Compose

Viewed 1988

I want to call my ViewModel's function every 5 seconds. What is the best way to do that in Jetpack Compose?

1 Answers

It depends when you want this behaviour to start and end.

This will run as long as your composable remains in the composition:

LaunchedEffect(Unit) {
    while(true) {
        vm.someMethod()
        delay(5000)
    }
}
Related