Skip to content

Instantly share code, notes, and snippets.

@hamrammi
Last active August 29, 2015 14:18
Show Gist options
  • Save hamrammi/66091cfa3f7977788c07 to your computer and use it in GitHub Desktop.
Save hamrammi/66091cfa3f7977788c07 to your computer and use it in GitHub Desktop.
throttle.js
function throttle (fn, threshold) {
var last, now, context, deferTimer, args
function exec() {
last = now
fn.apply(context, args)
}
return function () {
context = this
now = +new Date
args = arguments
if (last && now < last + threshold) {
clearTimeout(deferTimer)
deferTimer = setTimeout(exec, threshold)
} else {
exec()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment