+ 4

Is there any function to wait/sleep in javascript..?

18th Jan 2017, 6:50 PM
Ashwaghosh Sadanshiv
Ashwaghosh Sadanshiv - avatar
4 Answers
+ 9
setTimeout(callback,time,param1,param2,
) <- something like "setInterval", gets executed only once after some delay


18th Jan 2017, 7:09 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 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");
19th Jan 2017, 9:17 AM
visph
visph - avatar
- 2
maybe there's sleep () setTimeout()
18th Jan 2017, 7:06 PM
Minovsky
Minovsky - avatar
- 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! });
18th Jan 2017, 7:13 PM
Minovsky
Minovsky - avatar