Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Created March 29, 2021 16:35
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 james2doyle/11ef19563e3d32fffca1ac23ed7d8d65 to your computer and use it in GitHub Desktop.
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
/**
* 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