Skip to content

Instantly share code, notes, and snippets.

@dmi3y
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmi3y/ba14b8fa83a3fe47cef4 to your computer and use it in GitHub Desktop.
Save dmi3y/ba14b8fa83a3fe47cef4 to your computer and use it in GitHub Desktop.
Countdowns
function setCountDown(n) {
n = n || 5;
for ( var i = 0; i <= n; i++ ) {
(function() {
var j = n - i;
setTimeout(function() {
console.log(j);
}, 1000 * i);
}());
}
}
function* countdown(num) {
while (true) {
yield num--;
}
}
function setCountDown(n) {
n = n || 5;
var cd = countdown(n);
for(var i = 0; i <= n; i++) {
setTimeout(function() {
console.log(cd.next().value);
}, 1000 * i);
}
}
function setCountDown(n) {
n = n || 5;
(function cd(i) {
if ( i >= 0 ) {
setTimeout(function() {
console.log(i);
cd(--i);
}, 1000);
}
}(n));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment