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, that 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, that I'm using AJAX to retrieve some data, but it's a very fast pull, taking almost no time at all, but I want the user to see the processing animation for a couple of seconds (for psychological effect... it's a sort of quiz).
Where is the native Javascript Sleep Function? or what is the best way to implement such a function?