Skip to content

Instantly share code, notes, and snippets.

@giacomoferretti
Created April 3, 2023 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giacomoferretti/55b21c8d3d6f14836b4aa21b1677e14d to your computer and use it in GitHub Desktop.
Save giacomoferretti/55b21c8d3d6f14836b4aa21b1677e14d to your computer and use it in GitHub Desktop.
JavaScript throttle and debounce arrow functions
const throttle = (func, delay) => {
let timeoutId;
return (...args) => {
if (!timeoutId) {
timeoutId = setTimeout(() => {
func.apply(this, args);
timeoutId = null;
}, delay);
}
};
};
const debounce = (func, delay) => {
let timeoutId;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func.apply(this, args);
}, delay);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment