-
-
Save ncer/241e5e097519d2e8c7f7172fc7a50dc2 to your computer and use it in GitHub Desktop.
Smarter debouncing
This file contains hidden or 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 = 1) { | |
let timeout | |
return function (...args) { | |
clearTimeout(timeout) | |
timeout = setTimeout(() => fn.call(this, ...args), wait) | |
} | |
} |
This file contains hidden or 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) | |
var args = arguments | |
timeout = setTimeout(function () { | |
fn.apply(this, args) | |
}, (wait || 1)) | |
} | |
} |
This file contains hidden or 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
window.addEventListener('resize', debounce(function () { | |
}, 500)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment