Is there a Sleep/Pause/Wait function in JavaScript?

Richard picture Richard · Oct 21, 2011 · Viewed 821.3k times · Source

Is there a JavaScript function that simulates the operation of the sleep function in PHP — a function that pauses code execution for x milliseconds, and then resumes where it left off?

I found some things here on Stack Overflow, but nothing useful.

Answer

Diodeus - James MacFarlane picture Diodeus - James MacFarlane · Oct 21, 2011

You need to re-factor the code into pieces. This doesn't stop execution, it just puts a delay in between the parts.

function partA() {
  ...
  window.setTimeout(partB,1000);
}

function partB() {
   ...
}