DanWebb
7/7/2014 - 12:20 AM

setIntervalTimeout method created to solve issues with elements loading after page load where promises can't be applied.

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);