From 6790537997a48b3f2234b4898a19e205e8b0c3fd Mon Sep 17 00:00:00 2001 From: Mike Deverell Date: Tue, 23 Aug 2016 08:46:49 -0400 Subject: [PATCH] when using setTimeout, call callback immediately --- throttle.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/throttle.js b/throttle.js index cd55703..eee626b 100644 --- a/throttle.js +++ b/throttle.js @@ -1,5 +1,8 @@ function throttle(callback) { var running = false; + function resetRunning() { + running = false; + } return function () { if (running) { @@ -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 } }; }