Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put a Delay in Javascript

Tags:

javascript

I need to add a delay of about 100 miliseconds to my Javascript code but I don't want to use the setTimeout function of the window object and I don't want to use a busy loop. Does anyone have any suggestions?

like image 885
JGC Avatar asked Jul 26 '09 06:07

JGC


People also ask

How do I add a delay in JavaScript?

To delay a function call, use setTimeout() function. functionname − The function name for the function to be executed. milliseconds − The number of milliseconds. arg1, arg2, arg3 − These are the arguments passed to the function.

What is delay () in JavaScript?

Conclusion. setTimeout() is a method that will execute a piece of code after the timer has finished running. let timeoutID = setTimeout(function, delay in milliseconds, argument1, argument2,...); The delay is set in milliseconds and 1,000 milliseconds equals 1 second.

How do you delay 1 second in JavaScript?

To delay a function execution in JavaScript by 1 second, wrap a promise execution inside a function and wrap the Promise's resolve() in a setTimeout() as shown below. setTimeout() accepts time in milliseconds, so setTimeout(fn, 1000) tells JavaScript to call fn after 1 second.

Is there a wait command in JavaScript?

JavaScript do not have a function like pause or wait in other programming languages. setTimeout(alert("4 seconds"),4000); You need wait 4 seconds to see the alert.


2 Answers

Unfortunately, setTimeout() is the only reliable way (not the only way, but the only reliable way) to pause the execution of the script without blocking the UI.

It's not that hard to use actually, instead of writing this:

var x = 1;  // Place mysterious code that blocks the thread for 100 ms.  x = x * 3 + 2; var y = x / 2; 

you use setTimeout() to rewrite it this way:

var x = 1; var y = null; // To keep under proper scope  setTimeout(function() {     x = x * 3 + 2;     y = x / 2; }, 100); 

I understand that using setTimeout() involves more thought than a desirable sleep() function, but unfortunately the later doesn't exist. Many workarounds are there to try to implement such functions. Some using busy loops:

function sleep(milliseconds) {   var start = new Date().getTime();   for (var i = 0; i < 1e7; i++) {     if ((new Date().getTime() - start) > milliseconds){       break;     }   } } 

others using an XMLHttpRequest tied with a server script that sleeps for a amount of time before returning a result.

Unfortunately, those are workarounds and are likely to cause other problems (such as freezing browsers). It is recommended to simply stick with the recommended way, which is setTimeout()).

like image 149
Andrew Moore Avatar answered Sep 20 '22 17:09

Andrew Moore


If you're okay with ES2017, await is good:

const DEF_DELAY = 1000;  function sleep(ms) {   return new Promise(resolve => setTimeout(resolve, ms || DEF_DELAY)); }  await sleep(100); 

Note that the await part needs to be in an async function:

//IIAFE (immediately invoked async function expression) (async()=>{   //Do some stuff   await sleep(100);   //Do some more stuff })() 
like image 36
Nicholas Blasgen Avatar answered Sep 19 '22 17:09

Nicholas Blasgen