Create a Simple Delay Using setTimeout The standard way of creating a delay in JavaScript is to use its setTimeout method. For example: console. log("Hello"); setTimeout(() => { console.
Use of setTimeout() function: In order to wait for a promise to finish before returning the variable, the function can be set with setTimeout(), so that the function waits for a few milliseconds. Use of async or await() function: This method can be used if the exact time required in setTimeout() cannot be specified.
See if this works for you. Be sure to import the android.os.Handler
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// yourMethod();
}
}, 5000); //5 seconds
just add one-liner with lambda
(new Handler()).postDelayed(this::yourMethod, 5000);
edit for clarification: yourMethod
refers to the method which you want to execute after 5000 milliseconds.
you can use java handlers to achieve your task:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// Actions to do after 5 seconds
}
}, 5000);
for more information read the following url:
https://developer.android.com/reference/android/os/Handler.html
For import use : import android.os.Handler;
new Handler().postDelayed(new Runnable() {
public void run() {
// yourMethod();
}
}, 5000); //5 seconds
I use the following code (the same as you see before) for my android app, i need to wait some threads before start my new method. It works fine.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// yourMethod();
}
}, 5000); //5 seconds
This works for me:
val handler = Handler() handler.postDelayed({ // your code to run after 2 second }, 2000)
what I prefer is
(new Handler()).postDelayed(this::here is your method,2000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With