Is there an equivalent Javascript or jQuery sleep function?

I need something like this in javascript.

for (i = 0; i < 10; i++) {
    alert(i);
    // now sleep 1 sec
    sleep(1000);
}

Is there any built-in javascript or jquery for this?

Thanks!

+5
source share
7 answers

There is no such thing, directly. You will need to say javascript in order to wake up "something" after a while using setTimeout.

This "something" will be the code you plan to execute after sleep, of course.

From the example I found on the Internet :

function dothingswithsleep( part ) {
    if( part == 0 ) {
        alert( "before sleep" );
        setTimeout( function() { dothingswithsleep( 1 ); }, 1000 );
    } else if( part == 1 ) {
        alert( "after sleep" );
    }
}

. -, - : .

+5
+5

, ? , , . mutli-threaded javascript- , . javascript - , , YUI . - , . , , .

YUI.

http://developer.yahoo.com/yui/examples/event/index.html

YUI

var myEvent = new YAHOO.util.CustomEvent('fooEvent');
// subscribe a function to be called (first param) inside the scope of an object
// (second param).
myEvent.subscribe(function() {alert(this.count++);},{count:0},true);
setTimeout('myEvent.fire()',1000);

. ,

var myObj = {
    count:0,
    doSomething:function(){alert(this.count++);}
    loopFunc:function(){
        this.doSomething();
        setTimeout('myObj.loopFunc()',1000);
    }
}

, , .

, ajax script. , , . , , .

jQuery , Ajax?

setTimeout. , , , - , .

+2

/googled - javascript sleep/wait... , , javascript "RUN, DELAY, RUN"... , , : "RUN, RUN ( ), RUN" "RUN, RUN + delayed RUN"....

, : , ... ...:

//......................................... //example 1:

<html>
<body>
<div id="id1">DISPLAY</div>

<script>
//javascript sleep by "therealdealsince1982"; copyrighted 2009
//setInterval
var i = 0;

function run() {
    //pieces of codes to run
    if (i==0){document.getElementById("id1").innerHTML= "<p>code segment "+ i +" is ran</p>"; }
    if (i==1){document.getElementById("id1").innerHTML= "<p>code segment "+ i +" is ran</p>"; }
    if (i==2){document.getElementById("id1").innerHTML= "<p>code segment "+ i +" is ran</p>"; }
    if (i >2){document.getElementById("id1").innerHTML= "<p>code segment "+ i +" is ran</p>"; }
    if (i==5){document.getElementById("id1").innerHTML= "<p>all code segment finished running</p>"; clearInterval(t); } //end interval, stops run
    i++; //segment of code finished running, next...
}

t=setInterval("run()",1000);

</script>
</body>
</html>

//.................................... //example 2:

<html>
<body>
<div id="id1">DISPLAY</div>

<script>
//javascript sleep by "therealdealsince1982"; copyrighted 2009
//setTimeout
var i = 0;

function run() {
    //pieces of codes to run, can use switch statement
    if (i==0){document.getElementById("id1").innerHTML= "<p>code segment "+ i +" ran</p>"; sleep(1000);}
    if (i==1){document.getElementById("id1").innerHTML= "<p>code segment "+ i +" ran</p>"; sleep(2000);}
    if (i==2){document.getElementById("id1").innerHTML= "<p>code segment "+ i +" ran</p>"; sleep(3000);}
    if (i==3){document.getElementById("id1").innerHTML= "<p>code segment "+ i +" ran</p>";} //stops automatically
    i++;
}

function sleep(dur) {t=setTimeout("run()",dur);} //starts flow control again after dur

run(); //starts flow
</script>
</body>
</html>
+2

: " setTimeout, ", , . , , . .

for (i = 0; i < 10; i++) {
    alert(i);
    // now sleep 1 sec
    sleep(1000);
}

function recursive( i, max )
{
    if ( i > max ) return;
    alert( i );
    i = i + 1;
    setTimeout( function(){ recursive(i, max); }, 1000 );
}

recursive(1, 10);

: http://jsfiddle.net/q76Qa/

+1
source
function sleep(delay) { 
    var start = new Date().getTime(); 
    while (new Date().getTime() < start + delay); 
} 
0
source

Well, if you have installed PHP on a web server, you can use $.loador similar to call a PHP file, which has only sleep(int)...

0
source

All Articles