Last active
December 31, 2015 03:59
-
-
Save sebnilsson/7931165 to your computer and use it in GitHub Desktop.
Convert function to throttle function
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
function throttleFn(fn, wait, context) { | |
wait = wait || defaultThrottleWait; | |
var last, timeoutId; | |
return function() { | |
var ctx = (context || this), | |
now = +new Date, | |
args = arguments; | |
if (last && now < (last + wait)) { | |
clearTimeout(timeoutId); | |
timeoutId = setTimeout(function() { | |
last = now; | |
}, wait); | |
} else { | |
last = now; | |
fn.apply(ctx, args); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment