Javascript Sleep Function

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.

 

6 Responses to “Javascript Sleep Function”

  1. Mark says:

    Hi Mike,

    I couldn’t find anywhere to contact you so i thought i would do it through here. I know your blog is mainly on coding but noticed you posted some great tech posts such as the new mac video and thought a new video piece i am working on might be of interest to you. Its about a dysfunctional managers resistance to installing an ERP system. Although the video is really funny it does highlight some serious points.

    http://www.k3scs.com/the-factory/

    I thought it would point it out to you and if you have any feedback please let us know. Please also feel free to use the video.

    Thanks

    Mark

  2. Harish says:

    hi,

    Thanks a lot for the code. I think lots of people are looking for this. Btw where do I find the class file for this?

    regards,
    Harish

  3. Al says:

    This is a terrible implementation. The while loop will completely dominate the CPU and hang the browser until sleep is done. On Firefox, if you sleep for longer than 5 or 10 seconds, this will cause an ugly warning dialog because the browser will think the script is stuck in an infinite loop.

    There is no JavaScript sleep() function. (Which I find to be a huge flaw with the langauge.) Instead, you should use setTimeout(). Google “JavaScript sleep” to find what other people have said on this topic, but a loop that polls the current time is NOT what you want.

  4. therealdealsince1982 says:

    I have searched/googled quite a few webpages on javascript sleep/wait… 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…

  5. AGreenhill says:

    This is not a solution for me… I’m trying to avoid the IE script warning that occurs when a few million javascript operations occur… this can be done by setTimeout() but I seem to also require a true javascript sleep() function. Your jsleep() routine doesn’t help since it simply chugs through a loop at lightning speed. Have you tried your jsleep() on IE for a few seconds? I have little doubt it craps out.