How do i call a function after a delay in Kotlin
Delay Function in Kotlin: Call a function after a specific delay using Java Timer Class in Kotlin. Explore examples and best practices with RRTutors.
Published September 02, 2022
In this kotlin example we will cover call a function after a delay. Using Java Timer class we will pause method call after some delay by shedule() method.
Here is the example
package com.example.kotlinexamples import java.util.Timer import kotlin.concurrent.schedule fun main(args: Array<String>) { // Execution starting point println("Hello world!!") // Delay of 5 sec Timer().schedule(3000){ //calling a function methodCallWithDelay() } } fun methodCallWithDelay(){ println("Method called after delay") } |
After execute this file first it will print and after 3 seconds will execute methodCallWithDelay() and print "Method called after delay"
Output:
Hello world!! |