Skip to content

Commit

Permalink
when using setTimeout, call callback immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Deverell committed Aug 23, 2016
1 parent 72358d5 commit 6790537
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions throttle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function throttle(callback) {
var running = false;
function resetRunning() {
running = false;
}

return function () {
if (running) {
Expand All @@ -10,13 +13,14 @@ function throttle(callback) {
var args = arguments;
function frameHandler() {
callback.apply(undefined, args);
running = false;
resetRunning();
}

if (requestAnimationFrame) {
requestAnimationFrame(frameHandler);
if ('requestAnimationFrame' in window) {
window.requestAnimationFrame(frameHandler);
} else {
setTimeout(frameHandler, 1000 / 60); // 60 fps
callback.apply(undefined, args);
window.setTimeout(resetRunning, 1000 / 60); // 60 fps
}
};
}
Expand Down

0 comments on commit 6790537

Please sign in to comment.