Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sleep in javascript - no setTimeout [duplicate]

Tags:

javascript

All those setTimeout answers here don't work!

I just want to wait a few seconds between two functions, like this:

do_fn1();

wait(5000);

do_fn2();
like image 542
Frodo Avatar asked Jun 26 '11 13:06

Frodo


2 Answers

From phpied.com:

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}
like image 51
planestepper Avatar answered Oct 08 '22 20:10

planestepper


I don't think you can. You'll probably have to

do_fn1();
window.setTimeout(do_fn2, 5000);
like image 37
Andreas Jansson Avatar answered Oct 08 '22 21:10

Andreas Jansson