Skip to content

Instantly share code, notes, and snippets.

@kutyel
Forked from vincentorback/debounce-es2015.js
Last active June 14, 2017 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kutyel/6e037528b802933dd9cb5d5e0aa01f08 to your computer and use it in GitHub Desktop.
Save kutyel/6e037528b802933dd9cb5d5e0aa01f08 to your computer and use it in GitHub Desktop.
Smarter debouncing
export function debounce(fn, wait = 200) {
let timeout;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => fn.call(this, ...args), wait);
}
}
function debounce(fn, wait) {
var timeout;
return function () {
clearTimeout(timeout);
timeout = setTimeout(function () {
fn.apply(this, arguments)
}, (wait || 200));
}
}
function doSomething() {}
// vanilla
window.addEventListener('resize', debounce(doSomething, 500));
// es6
window.addEventListener('resize', debounce(doSomething, 500));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment