如何在 Kotlin 延迟后调用函数? [英] How to call a function after delay in Kotlin?
本文介绍了如何在 Kotlin 延迟后调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如题,在Kotlin
中,有没有延迟(例如1秒)后调用函数的方法?
As the title, is there any way to call a function after delay (1 second for example) in Kotlin
?
推荐答案
您可以使用 日程
inline fun Timer.schedule(
delay: Long,
crossinline action: TimerTask.() -> Unit
): TimerTask (source)
示例(感谢@Nguyen Minh Binh - 在这里找到:http://jamie.mccrindle.org/2013/02/exploring-kotlin-standard-library-part-3.html)
example (thanks @Nguyen Minh Binh - found it here: http://jamie.mccrindle.org/2013/02/exploring-kotlin-standard-library-part-3.html)
import java.util.Timer
import kotlin.concurrent.schedule
Timer("SettingUp", false).schedule(500) {
doSomething()
}
这篇关于如何在 Kotlin 延迟后调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文