Skip to content

Instantly share code, notes, and snippets.

@alsotang
Last active August 30, 2016 09:19
Show Gist options
  • Save alsotang/9c85686ffb886e63f4c72ccca24c55a8 to your computer and use it in GitHub Desktop.
Save alsotang/9c85686ffb886e63f4c72ccca24c55a8 to your computer and use it in GitHub Desktop.
function forceInterval(fn, time) {
var lastRunTime = new Date();
return setInterval(function () {
var currentTime = new Date()
if ((new Date - lastRunTime) < time) {
fn();
} else {
var count = ~~((new Date - lastRunTime) / time)
for (var i = 0; i < count; i++) {
fn()
}
}
lastRunTime = currentTime;
}, time)
}
var count = 0;
setInterval(function () {
count++;
console.log(count)
}, 100)
var startTime = +new Date()
while ((new Date - startTime) > 2000) {
}
setTimeout(function () {
console.log('after 1200ms:', count)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment