Last active
August 29, 2015 14:03
-
-
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.
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
// 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