Yes, I know - that question has thousands of answers. please, don't tell me about setTimeout
method because - 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 aren't 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).