Skip to content

Instantly share code, notes, and snippets.

@DanWebb
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanWebb/8b688b31492632b38aea to your computer and use it in GitHub Desktop.
Save DanWebb/8b688b31492632b38aea to your computer and use it in GitHub Desktop.
setIntervalTimeout method created to solve issues with elements loading after page load where promises can't be applied.
// call a callback function every *interval* until *stopTime* or until (bool)true is
// returned from the callback function
function setIntervalTimeout(callback, interval, stopTime) {
var i;
i = setInterval(function() {
if(callback()) clearInterval(i);
}, interval);
setTimeout(function() {
clearInterval(i);
}, stopTime);
}
// example usage
var iterate = 1;
setIntervalTimeout(function(){
console.log(iterate);
if(iterate===3) return true;
iterate++;
}, 1000, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment