I’ll make this short and quick. I’ve been searching the web for a Javascript sleep function that works and came across this. It was presented as a class but what I needed was just a function so I rewrote the code a bit. Here’s the end result of my Javascript sleep function which I named, well, jsleep.
function jsleep(s){
s=s*1000;
var a=true;
var n=new Date();
var w;
var sMS=n.getTime();
while(a){
w=new Date();
wMS=w.getTime();
if(wMS-sMS>s) a=false;
}
}
// how to use
alert('in the beginning there was a pause...');
jsleep(3);
alert('3 seconds after... the pause was gone.');
I’ve tested it in Google Chrome, Firefox 3 and Internet Explorer 7. Works like a charm.