Skip to content

Instantly share code, notes, and snippets.

@AntonioHReyes
Last active December 4, 2021 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AntonioHReyes/73a3184362dc8d9a65157070695d87cb to your computer and use it in GitHub Desktop.
Save AntonioHReyes/73a3184362dc8d9a65157070695d87cb to your computer and use it in GitHub Desktop.
SetTimeout Extension Function for Android
fun setTimeOut(handleFunction: () -> Unit, delay: Long): CountDownTimer {
val timer = object: CountDownTimer(delay, 1000){
override fun onFinish() {
CoroutineScope(Dispatchers.Main).launch {
handleFunction()
cancel()
}
}
override fun onTick(p0: Long) {}
}
timer.start()
return timer
}
fun clearTimeOut(timer: CountDownTimer){
try {
timer.cancel()
}catch (e: Exception){
Log.e("Error", e.toString())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment