Created
September 10, 2015 12:48
-
-
Save LuisValdesZero/0c6cfa5c3a0473546e83 to your computer and use it in GitHub Desktop.
A recursive task to update DOM elements that are loaded dynamically.
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 many seconds does the interval have 'time' seconds | |
var time = 5; | |
// The recursive call will be made every 'timeout' milliseconds | |
var timeout = 500; | |
// this task will be executed in every call to 'recursive' | |
var task = function(level){ | |
console.log("Hello world, level is " + level); | |
}; | |
// this is an example of a stop condition for the recursive call. | |
var counter = 0; | |
var condition = function(val) { | |
if (val == 10) | |
counter++; | |
return counter > 2; | |
}; | |
var recursive = function(level){ | |
level = level || 0; | |
task(level); | |
if(condition(level)) { | |
console.log("Recursive task is finished!"); | |
}else{ | |
setTimeout(function(){ | |
recursive(level == (time*1000 / timeout) ? 0 : level + 1 ); | |
}, timeout); | |
} | |
}; | |
recursive(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment