I want something like this in javascript.
for (i = 0; i < 10; i++) {
alert(i);
// now sleep 1 sec
sleep(1000);
}
is there a built in Javascript or Jquery for this?
Thank you!
There's no such thing, directly. You would have to tell javascript to wake 'something' up after some time using setTimeout
.
This 'something' would be the code that you plan to execute after the sleep, of course.
From an 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" );
}
}
But that's fragile design. You better rethink your business logic to really do something after a second: call a different function instead of using these contrived helper variables.