- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to call a function after a delay in Kotlin?
Kotlin is based on Java, hence we can use Java-based library functions to delay a function call. In this article, we will be using a Java library function to delay the function call using Timer() and schedule().
Example
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(5000){ //calling a function newMethod() } } fun newMethod(){ println("Delayed method call!") }
Output
Once executed, the above piece of code will yield the following output −
Hello world!! Delayed method call!
Advertisements