mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-06 19:35:53 -05:00
ef96426ca0
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
28 lines
690 B
TypeScript
28 lines
690 B
TypeScript
export type DebouncedFunction<T extends (...args: any[]) => void> = ((...args: Parameters<T>) => void) & {
|
|
cancel: () => void;
|
|
};
|
|
|
|
export const debounce = <T extends (...args: any[]) => void>(
|
|
callback: T,
|
|
delay: number
|
|
): DebouncedFunction<T> => {
|
|
let timeoutId: ReturnType<typeof setTimeout> | undefined;
|
|
|
|
const debounced = ((...args: Parameters<T>) => {
|
|
if (timeoutId !== undefined) {
|
|
clearTimeout(timeoutId);
|
|
}
|
|
|
|
timeoutId = setTimeout(() => callback(...args), delay);
|
|
}) as DebouncedFunction<T>;
|
|
|
|
debounced.cancel = () => {
|
|
if (timeoutId !== undefined) {
|
|
clearTimeout(timeoutId);
|
|
timeoutId = undefined;
|
|
}
|
|
};
|
|
|
|
return debounced;
|
|
};
|