Created
February 28, 2012 04:06
-
-
Save pwhelan/1929317 to your computer and use it in GitHub Desktop.
Tail Recursion to infinite timed loop in Node.js... with Closures!
This file contains 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
function loop() { | |
var start_date = Date.now() | |
var old_date = start_date | |
var seconds_running = 120 | |
var intervalId = setInterval(1000, function() { | |
new_date = Date.now() | |
// Execute this for just 30 seconds | |
if( (new_date - start_date) < 1000 * seconds_running) { | |
var diff_tick = new_date - old_date | |
console.log('tick with difference : ' + diff_tick) | |
old_date = new_date | |
} | |
else { | |
console.log('Reached ' + seconds_running +' seconds at: ' + Date() ) | |
clearInterval(intervalId); | |
} | |
}); | |
} | |
loop(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment