Skip to content

Instantly share code, notes, and snippets.

@ncer
Forked from vincentorback/debounce-es2015.js
Created February 17, 2021 11:24
Show Gist options
  • Save ncer/241e5e097519d2e8c7f7172fc7a50dc2 to your computer and use it in GitHub Desktop.
Save ncer/241e5e097519d2e8c7f7172fc7a50dc2 to your computer and use it in GitHub Desktop.
Smarter debouncing
export function debounce (fn, wait = 1) {
let timeout
return function (...args) {
clearTimeout(timeout)
timeout = setTimeout(() => fn.call(this, ...args), wait)
}
}
function debounce (fn, wait) {
var timeout
return function () {
clearTimeout(timeout)
var args = arguments
timeout = setTimeout(function () {
fn.apply(this, args)
}, (wait || 1))
}
}
window.addEventListener('resize', debounce(function () {
}, 500))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment