Always wondered that why does Javascript do not have some extraordinary functions like trim, wait, sleep etc. Oh well, i think at that time they din bother to add them or din feel the requirement. 
I was working on a script last week and somehow i wondered if i can trick around such that my JavaScript wait for few seconds and after that runs the awesome code. There are two ways of doing that, as follows
The customized Sleep way :
<html> <head> <script type="text/javascript"> function sleep(ms) { var dt = new Date(); dt.setTime(dt.getTime() + ms); while (new Date().getTime() < dt.getTime()); } </script> </head> <body> <script> sleep(4000); alert("we are done"); </script> </body>
Using Javascript’s setTimeout func :
setTimeout("alert ('called from setTimeout()');",4000);
Difference between setTimeout and sleep()
The difference between the above function is that sleep will halt the script and will not allow the script to execute further lines of code. However when setTimeout is called, it won’t halt the further execution of the script, instead it will execute after the specified interval of time and also the other functions (if any) will keep on executing.
if you want to check this scenario .. just copy paste the below code into the body section and comment, sleep and setTimeout function calls one by one to notice the difference.
<script> //sleep(4000); setTimeout("alert ('called from setTimeout()');",4000); alert("we are done"); </script>
Hope this helps.
Cheers !!
Sachin Khosla




Hi Sachin,
The problem with your approach is that it is a busy-wait. IIRC, some browsers will stall the user interface completely while the wait is executing, while others will be sluggish.
Better to wrap the second part in a function and call it with
settimeout(“secondpart();”,4000); at the end of the first part.
Regards
Ian
Hiya Ian,
Exactly FFX stops the user interface for that while, but i just meant to show ppl. that this can be done. Of course the setTimeout is better
Thanks for dropping the comment
hi Sachin
the problem with sleep and setTimeout is that in a conditon where javascript calls server side code (like in most of todays world Ajax apps) we cant predict t how much time would a DB or any other server would take
window.setTimeout(“A()”, 4000);
B();
the method A() hits DB but if the reterival of data from DB takes more than 4 sec B() would be called and application can behave in unpredicted manner , so the right approach is to use code like :
A(parametersLIst,B)
and to use setTimeout very less as this method ensures proper Callback and sequence in which js methods are called
Regards
Amit
Yeah sounds good, but when you interact with the server using Ajax application, u probably do not need settimeout, instead you can use sleep with any of the server side language. That is neat
I have searched/googled quite a few webpages on javascript sleep… and there is NO answer if you want javascript to “RUN, DELAY, RUN”… what most people got was either, “RUN, RUN(useless stuff), RUN” or “RUN, RUN + delayed RUN”….
So I ate some burgers and got thinking:::
here is a solution that works… but you have to chop up your running codes…:::
replace <.. with < to run..
//…………………………………..
//example1:
DISPLAY
//javascript sleep by “therealdealsince1982″; copyrighted 2009
//setInterval
var i = 0;
function run() {
//pieces of codes to run
if (i==0){document.getElementById(“id1″).innerHTML= “code segment “+ i +” is ran”; }
if (i==1){document.getElementById(“id1″).innerHTML= “code segment “+ i +” is ran”; }
if (i==2){document.getElementById(“id1″).innerHTML= “code segment “+ i +” is ran”; }
if (i >2){document.getElementById(“id1″).innerHTML= “code segment “+ i +” is ran”; }
if (i==5){document.getElementById(“id1″).innerHTML= “all code segment finished running”; clearInterval(t); } //end interval, stops run
i++; //segment of code finished running, next…
}
t=setInterval(“run()”,1000);
//………………………………
//example2:
DISPLAY
//javascript sleep by “therealdealsince1982″; copyrighted 2009
//setTimeout
var i = 0;
function flow() {
run(i);
i++; //code segment finished running, increment i; can put elsewhere
sleep(1000);
if (i==5) {clearTimeout(t);} //stops flow, must be after sleep()
}
function run(segment) {
//pieces of codes to run, can use switch statement
if (segment==0){document.getElementById(“id1″).innerHTML= “code segment “+ segment +” is ran”; }
if (segment==1){document.getElementById(“id1″).innerHTML= “code segment “+ segment +” is ran”; }
if (segment==2){document.getElementById(“id1″).innerHTML= “code segment “+ segment +” is ran”; }
if (segment >2){document.getElementById(“id1″).innerHTML= “code segment “+ segment +” is ran”; }
}
function sleep(dur) {t=setTimeout(“flow()”,dur);} //starts flow control again after dur
flow(); //starts flow
//……………………………….
//example3:
DISPLAY
//javascript sleep by “therealdealsince1982″; copyrighted 2009
//setTimeout, switch
var i = 0;
function flow() {
switch(i)
{
case 0:
run(i);
sleep(1000);
break;
case 1:
run(i);
sleep(2000);
break;
case 5:
run(i);
clearTimeout(t); //stops flow
break;
default:
run(i);
sleep(3000);
break;
}
}
function run(segment) {
//pieces of codes to run, can use switch statement
if (segment==0){document.getElementById(“id1″).innerHTML= “code segment “+ segment +” is ran”; }
if (segment==1){document.getElementById(“id1″).innerHTML= “code segment “+ segment +” is ran”; }
if (segment==2){document.getElementById(“id1″).innerHTML= “code segment “+ segment +” is ran”; }
if (segment >2){document.getElementById(“id1″).innerHTML= “code segment “+ segment +” is ran”; }
i++; //current segment of code finished running, next…
}
function sleep(dur) {t=setTimeout(“flow()”,dur);} //starts flow control again after dur
flow(); //starts flow control for first time…