JavaScript sleep
Posted
by
Diazath
on Stack Overflow
See other posts from Stack Overflow
or by Diazath
Published on 2011-01-08T13:10:29Z
Indexed on
2011/01/08
13:53 UTC
Read the original article
Hit count: 174
JavaScript
|sleep
yes, i know - that question has thousands of answers. please, don't tell me about "setTimeout" method becasuse - yes, everything is possible with that but not so easy as using sleep() method. for example:
function fibonacci(n) {
console.log("Computing Fibonacci for " + n + "...");
var result = 0;
//wait 1 second before computing for lower n
sleep(1000);
result = (n <= 1) ? 1 : (fibonacci(n - 1) + fibonacci(n - 2));
//wait 1 second before announcing the result
sleep(1000);
console.log("F(" + n + ") = " + result);
return result;
}
if you know how to get the same result using setTimeout - tell me ;) fibanacci is pretty easy task, because there not more than 2 recursions, but how about n-recursions (like fib(1) + fib(2) + .. + fib(n) and sleep after every "+"? nah, sleep would be muuuuuch easier. but still i can't get working example of implementing it. while (curr - start < time) { curr = (...) } is tricky, but it won't work (just stops my browser and then throw all console.logs at once).
© Stack Overflow or respective owner