Skip to content

Instantly share code, notes, and snippets.

@GuillermoPena
Created May 28, 2014 09:59
Show Gist options
  • Save GuillermoPena/a46ebde761c52e57a14b to your computer and use it in GitHub Desktop.
Save GuillermoPena/a46ebde761c52e57a14b to your computer and use it in GitHub Desktop.
NodeJS - Scheduled Loop
// How wait inside of loop...
// Countdown
var counter = 10
var countdown = function() {
console.log(counter)
counter--
return (counter > 0)
}
// Run callback until it return false (when countdown end in this case)
function scheduledLoop(cb) {
if (cb()) setTimeout(scheduledLoop, 1000, countdown)
}
scheduledLoop(countdown)
// Another way to do it is using setInterval, and when countdown ends,
// uning clearInterval (but this one, I thnk that is more elegant)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment