Skip to content

Instantly share code, notes, and snippets.

@CarmeloRicarte
Created April 22, 2025 14:16
Show Gist options
  • Save CarmeloRicarte/64f558191d400baecb74720fb4059d21 to your computer and use it in GitHub Desktop.
Save CarmeloRicarte/64f558191d400baecb74720fb4059d21 to your computer and use it in GitHub Desktop.
Debounce with Typescript and example of use
// 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