Created
April 22, 2025 14:16
-
-
Save CarmeloRicarte/64f558191d400baecb74720fb4059d21 to your computer and use it in GitHub Desktop.
Debounce with Typescript and example of use
This file contains hidden or 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
// biome-ignore lint/suspicious/noExplicitAny | |
export function debounce<T extends (...args: any[]) => void>(func: T, timeout = 300): (...args: Parameters<T>) => void { | |
let timer: NodeJS.Timeout | undefined; | |
return (...args: Parameters<T>) => { | |
clearTimeout(timer); | |
timer = setTimeout(() => { | |
func(...args); | |
}, timeout); | |
}; | |
} | |
// use case in a search input in React | |
const debouncedOnSearch = debounce((e: FormEvent, value: string) => { | |
e.preventDefault(); | |
onSearch({ query: value }); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment