Created
March 29, 2021 16:35
-
-
Save james2doyle/11ef19563e3d32fffca1ac23ed7d8d65 to your computer and use it in GitHub Desktop.
Debounce creates and returns a new version of the passed function that will run only after the timeout has elapsed
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
/** | |
* Debounce creates and returns a new version of the passed function | |
* that will run only after the timeout has elapsed | |
*/ | |
function debounce(fn: Function, wait: number = 0) { | |
let timeout: ReturnType<typeof setTimeout> | null = null; | |
return (...args: any) => { | |
if (timeout) { | |
clearTimeout(timeout); | |
} | |
timeout = setTimeout(() => { | |
fn(...args); | |
}, wait); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment