Skip to content

Instantly share code, notes, and snippets.

@vancouverwill
Last active December 17, 2015 17:29
Show Gist options
  • Save vancouverwill/5646673 to your computer and use it in GitHub Desktop.
Save vancouverwill/5646673 to your computer and use it in GitHub Desktop.
javascript limited time interval using loop
function setTimedOutInterval(totalTime, intervalTime, callBackFunction) {
var noOfLoops = totalTime / intervalTime,
counter = 0;
intervalId = setInterval(function () {
console.log('check');
console.log(counter);
if (counter <= noOfLoops) {
counter++;
} else {
clearInterval(intervalId);
console.log('start callBackFunction');
callBackFunction();
}
if (finished === true) {
clearInterval(intervalId);
callBackFunction();
}
}, intervalTime);
}
setTimedOutInterval(4000, 500, finished);
function finished() {
console.log('finished!!');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment