Skip to content

Instantly share code, notes, and snippets.

@vertexvaar
Created February 3, 2022 17:59
Show Gist options
  • Save vertexvaar/32a755941954d9bb639e06cdf979765d to your computer and use it in GitHub Desktop.
Save vertexvaar/32a755941954d9bb639e06cdf979765d to your computer and use it in GitHub Desktop.
Debounce JS
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