I searched all around looking for a way to do an actual sleep function (Thread.Sleep, wait, sleep(), take your pick...) in JavaScript, and there were many posts around that use setInterval
or setTimeout
, there were none that actually did a real sleep.
The problem is, the setTimeout
function immediately returns control to its container after it sets the timer. So, if you setTimeout(funct(),1000)
, and its container runs for 5 seconds longer, the last 4 seconds will see BOTH functions running at the same time. Now, arguably, this is good in some cases, perhaps even most cases as it relates to JavaScript applications. However, in an application I've been working on recently, I needed to actually sleep for a couple seconds. Literally make the script stall. The reason is, I'm using AJAX to retrieve some data, but its a very fast pull, taking almost no time at all, but we WANT the user to see the processing animation for a couple seconds (for psychological affect... its a sort of quiz).
So, here's what I did. Its actually pretty obvious in hindsight, so I'm surprised I didn't find any one else using this technique. This is actually an excerpt from a larger class file. Anyway, this is mostly for posterity, but comments are welcome. Oh, and I'm sure there are good reasons for not normally wanting to do this, also, I suppose if you ran this for too long, browsers would throw errors or crash. I only need it for 3-5 seconds, so this isn't a problem for me.
this.Sleep = function ZZzzzZZzzzzzzZZZz(naptime){
naptime = naptime * 1000;
var sleeping = true;
var now = new Date();
var alarm;
var startingMSeconds = now.getTime();
alert("starting nap at timestamp: " + startingMSeconds + "\nWill sleep for: " + naptime + " ms");
while(sleeping){
alarm = new Date();
alarmMSeconds = alarm.getTime();
if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
}
alert("Wakeup!");
}
This is part of a class function, and since I also set var my = this;
right at the top of any JS class I write, this would be called privately as my.Sleep(5)
, and publicly like myObject.Sleep(5)
(assuming you named the object myObject, which you probably wouldn't 😉)
setTimeout
, you'd still have two functions running at the same time, even if one is an syncXMLHTTP
request. JavaScript isn't multi-threaded, so I've found its generally best not to force it to do more than one thing at a time, if possible. — CarnixsetTimeout
to a controll re-enabling function? The while loop sure does look alot easier to work with than setTimeout when it's all in one place like that. Beats the hell out of "just trust me" if you know what I mean. Nice stuff carnix! 😎 — joebertonUnload
function that needs an extra second or two to process before the window is destroyed. In this case,setTimeout
, and many other solutions failed and I needed a method to force the window to stay open and let theonUnload
function do its business. It's a quick and dirty solution, but it sure beats rewriting someone else's code entirely. — dreaken667