Giới thiệu
Kotlin chạy trên JVM, có thể dùng thư viện Java để delay gọi hàm. Ví dụ dùng Timer() và schedule().
Ví dụ
import java.util.Timer
import kotlin.concurrent.schedule
fun main(args: Array<String>) {
println("Hello world!!")
// Delay 5 giây
Timer().schedule(5000) {
newMethod()
}
}
fun newMethod() {
println("Delayed method call!")
}
Kết quả
Hello world!! Delayed method call!
Lưu ý: kotlin.concurrent.schedule là extension function cho Timer, đơn giản hóa cú pháp.