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); } }