Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Created October 27, 2013 18:13
Show Gist options
  • Save CMCDragonkai/7185919 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/7185919 to your computer and use it in GitHub Desktop.
JS: createConditionalIntervalFunction - Creates a function that repeats its own execution repeatedly or until a condition evaluates to true. Useful for asynchronously checking some condition before running something else.
var createConditionalIntervalFunction = function(condition, callback, interval, repeat = true){
var intervalFunc = function(){
setTimeout(function(){
//if repeat is true, this intervalFunc will be repeated continually
//if repeat is false, this intervalFunc will only be repeated until the condition() is true
if(condition()){
callback();
}else{
if(!repeat){
intervalFunc();
}
}
if(repeat){
intervalFunc();
}
}, interval);
};
return intervalFunc;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment