Last active
May 17, 2024 01:55
-
-
Save rlueder/4d89d81b9ce3f2a22b5ab5faf9e1818e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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