Created
February 14, 2022 16:00
-
-
Save wajahatkarim3/61c87f17279707cfb410845dfcabb792 to your computer and use it in GitHub Desktop.
Timer in Composables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun SomeComposableViewHere() { | |
| val scope = rememberCoroutineScope() | |
| scope.launch { | |
| try { | |
| startTimer(flipDurationMs.toLong()) { | |
| // Timer Ended. Do something here | |
| } | |
| } catch(ex: Exception) { | |
| // Timer Cancelled. | |
| ex.printStackTrace() | |
| } | |
| } | |
| } | |
| private suspend fun startTimer(time: Long, onTimerEnd: () -> Unit) { | |
| delay(timeMillis = time) | |
| onTimerEnd() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment