Kotlin: How to run a function with delay using extension function

Viewed 2508

I'm trying to figure out how to use an extension function to run any method with a delay, but can't seem to figure it out.

I am trying something like below where I have a function and I want a handler to delay the execution by a certain timeInterval:

functionX().withDelay(500)
functionY().withDelay(500)

private fun Unit.withDelay(delay: Int) {
  Handler().postDelayed( {this} , delay)}

private fun Handler.postDelayed(function: () -> Any, delay: Int) {
  this.postDelayed(function, delay)}

Anyone?

4 Answers
Related