Created
October 27, 2013 18:13
-
-
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.
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
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