Skip to content

Instantly share code, notes, and snippets.

@fcurella
Created January 21, 2011 04:06
Show Gist options
  • Save fcurella/789222 to your computer and use it in GitHub Desktop.
Save fcurella/789222 to your computer and use it in GitHub Desktop.
// execute callback only after a pause in user input; the function returned
// can be used to handle an event type that tightly repeats (such as typing
// or scrolling events); it will execute the callback only if the given timout
// period has passed since the last time the same event fired
function createOnPause(callback, timeout, _this) {
return function(e) {
var _that = this;
if (arguments.callee.timer)
clearTimeout(arguments.callee.timer);
arguments.callee.timer = setTimeout(function() {
callback.call(_this || _that, e);
}, timeout);
}
}
// Use it like this:
document.addEventListener('scroll', createOnPause(function(e) {
// do something interesting here
}, 1500, this), false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment