Created
February 3, 2022 17:59
-
-
Save vertexvaar/32a755941954d9bb639e06cdf979765d to your computer and use it in GitHub Desktop.
Debounce JS
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
const centreElement = function () { | |
console.log('Debounced'); | |
} | |
const echoStuff = function () { | |
console.log('Stuff'); | |
} | |
const createDebounce = function (fnc, delayMs) { | |
var timeout = null; | |
return function () { | |
clearTimeout(timeout); | |
timeout = setTimeout(() => { | |
fnc(); | |
}, delayMs || 500); | |
}; | |
} | |
window.addEventListener('resize', createDebounce(centreElement, 200)); | |
window.addEventListener('click', createDebounce(echoStuff, 200)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment