+ 4
Is there any function to wait/sleep in javascript..?
4 Answers
+ 9
setTimeout(callback,time,param1,param2,âŠ) <- something like "setInterval", gets executed only once after some delayâŠâŠ
+ 1
@visph wrote in https://www.sololearn.com/Discuss/171574/delay :
function func() {
/* function body */
}
var delay = 2500; // millisecondes
window.setTimeout( func, delay );
/* rest of code running without waiting execution of 'func' */
If you need to WAIT instead DELAY, you must handle a way to simulate an end event for your function...
_____________________________________________________________________
You can pass arguments to the callback function, through the setTimeout arguments list:
window.setTimeout(alert,5000,"Five second delayed alert");
- 2
maybe there's
sleep ()
setTimeout()
- 2
or with this function
// sleep time expects milliseconds
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
// Usage!
sleep(500).then(() => {
// Do something after the sleep!
});