Skip to content

Instantly share code, notes, and snippets.

@rlueder
Last active May 17, 2024 01:55
Show Gist options
  • Save rlueder/4d89d81b9ce3f2a22b5ab5faf9e1818e to your computer and use it in GitHub Desktop.
Save rlueder/4d89d81b9ce3f2a22b5ab5faf9e1818e to your computer and use it in GitHub Desktop.
/**
* @name debounce
* @summary Debounces "func" within "wait".
* @see {@link https://davidwalsh.name/javascript-debounce-function}
* @see {@link https://www.freecodecamp.org/news/javascript-debounce-example/}
* @example <button onClick={debounce(() => console.log("1s"), 1000)}>Debounce</button>
* @param {Function} func
* @param {number} [wait]
* @returns {Function}
*/
const debounce = (func, wait = 250) => {
let timeout;
return () => {
clearTimeout(timeout);
timeout = setTimeout(() => {
func.apply(this);
}, wait);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment