3 ms·
I understand the scoping issue here, but the recursive solution is just all around better: function countdown(num) { if(num < 0) { return;
by sgk284 16y ago
I understand the scoping issue here, but the recursive solution is just all around better:
function countdown(num) {
if(num < 0) {
return;
}
setTimeout(function() {
alert(num);
countdown(num - 1);
}, 1000);
}
Note: I'm aware that the dropbox version shows the first alert without any delay, whereas this waits a second before showing anything. This is slightly different behavior, but arguably acceptable.
- abstractbill 16y agoYou can make the recursive solution work without any delay: function countdown(n) { if(n >= 0) { alert(n); setTimeout(function() {countdown(n-1);}, 1000); } }