How to call a function after delay in Kotlin?

前端 未结 11 1923
南笙
南笙 2020-11-30 19:07

As the title, is there any way to call a function after delay (1 second for example) in Kotlin?

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 19:49

    If you are looking for generic usage, here is my suggestion:

    Create a class named as Run:

    class Run {
        companion object {
            fun after(delay: Long, process: () -> Unit) {
                Handler().postDelayed({
                    process()
                }, delay)
            }
        }
    }
    

    And use like this:

    Run.after(1000, {
        // print something useful etc.
    })
    

提交回复
热议问题