Created
November 4, 2012 14:21
-
-
Save gotomypc/4012111 to your computer and use it in GitHub Desktop.
Tail Recursion in Node.js
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
| start_date = Date.now() | |
| old_date = start_date | |
| seconds_running = 120 | |
| console.log('Starting Program at: ' + Date()) | |
| loop_flag = true | |
| loop() | |
| function loop() { | |
| tick(); | |
| if(loop_flag) { | |
| timeOut = setTimeout(loop, 1000); | |
| } | |
| } | |
| function tick() { | |
| new_date = Date.now() | |
| // Execute this for just 30 seconds | |
| if( (new_date - start_date) < 1000 * seconds_running) { | |
| 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() ) | |
| loop_flag = false | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment