Skip to content

Instantly share code, notes, and snippets.

@kavichu
Created September 10, 2015 12:48
Show Gist options
  • Save kavichu/0c6cfa5c3a0473546e83 to your computer and use it in GitHub Desktop.
Save kavichu/0c6cfa5c3a0473546e83 to your computer and use it in GitHub Desktop.
A recursive task to update DOM elements that are loaded dynamically.
// 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