Skip to content

Instantly share code, notes, and snippets.

@marekdano
Created October 10, 2022 09:11
Show Gist options
  • Save marekdano/02a984ffbbca3be1539357ce6f553e82 to your computer and use it in GitHub Desktop.
Save marekdano/02a984ffbbca3be1539357ce6f553e82 to your computer and use it in GitHub Desktop.
function debounce(callback, timer) {
let timeoutId;
return (...args) => {
// save the current context (this)
const context = this;
// clear the existing timeout
clearTimeout(timeoutId);
// create a new timeout
timeoutId = setTimeout(() => {
callback.apply(context, args);
}, timer);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment