-
-
Save kutyel/6e037528b802933dd9cb5d5e0aa01f08 to your computer and use it in GitHub Desktop.
Smarter debouncing
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
export function debounce(fn, wait = 200) { | |
let timeout; | |
return function (...args) { | |
clearTimeout(timeout); | |
timeout = setTimeout(() => fn.call(this, ...args), wait); | |
} | |
} |
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 debounce(fn, wait) { | |
var timeout; | |
return function () { | |
clearTimeout(timeout); | |
timeout = setTimeout(function () { | |
fn.apply(this, arguments) | |
}, (wait || 200)); | |
} | |
} |
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 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