Skip to content

Instantly share code, notes, and snippets.

@treyhuffine
Last active October 19, 2023 22:45
Show Gist options
  • Save treyhuffine/2ced8b8c503e5246e2fd258ddbd21b8c to your computer and use it in GitHub Desktop.
Save treyhuffine/2ced8b8c503e5246e2fd258ddbd21b8c to your computer and use it in GitHub Desktop.
// Originally inspired by David Walsh (https://davidwalsh.name/javascript-debounce-function)
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// `wait` milliseconds.
const debounce = (func, wait) => {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
};
@sinisimattia
Copy link

So simple yet so useful, thanks a lot

@treyhuffine
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment