Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
68
rated 0 times [  72] [ 4]  / answers: 1 / hits: 37972  / 15 Years ago, fri, august 14, 2009, 12:00:00

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!


More From » jquery

 Answers
270

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.


[#98912] Tuesday, August 11, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
griffinr

Total Points: 242
Total Questions: 91
Total Answers: 105

Location: Indonesia
Member since Wed, Jul 7, 2021
3 Years ago
;