Reputation: 689
I want to call my ViewModel's function every 5 seconds. What is the best way to do that in Jetpack Compose?
Upvotes: 8
Views: 9022
Reputation: 246
LaunchedEffect(state.showCards) {
Log.d("gameScreen", "launcheffect: state.showCards")
if(state.showCards){
repeat(5){
Log.d("gameScreen", "count: ${it+1}")
delay(1000)
}
viewModel.hideCards()
}
}
This above code is for the case if you want to do something on some event(on button click) and after 5 second trigger a function to do something else with a count down
Upvotes: 1
Reputation: 789
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)
}
}
Upvotes: 25