Skip to content

Instantly share code, notes, and snippets.

@potch
Created September 25, 2014 23:25
Show Gist options
  • Save potch/03927c763b21c938b369 to your computer and use it in GitHub Desktop.
Save potch/03927c763b21c938b369 to your computer and use it in GitHub Desktop.
function Debounce(fn, ms) {
this.timeout = null;
this.fn = fn;
this.ms = ms;
}
Debounce.prototype.start = function () {
this.timeout = setTimeout(this.fn, this.ms);
};
Debounce.prototype.reset = function () {
this.abort();
this.start();
};
Debounce.prototype.abort = function () {
clearTimeout(this.timeout);
};
@potch
Copy link
Author

potch commented Oct 8, 2014

var debouncedScroll = new Debounce(function () {
  // do stuff
}, 200);
window.addEventListener('scroll', debouncedScroll.reset);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment