Created
May 28, 2014 09:59
-
-
Save GuillermoPena/a46ebde761c52e57a14b to your computer and use it in GitHub Desktop.
NodeJS - Scheduled Loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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