Files
triggerdotdev--trigger.dev/apps/webapp/app/hooks/useThrottle.ts
T
2026-07-13 13:32:57 +08:00

24 lines
581 B
TypeScript

import { useEffect, useRef } from "react";
export function useThrottle(fn: (...args: any[]) => void, duration: number) {
const timeout = useRef<ReturnType<typeof setTimeout>>();
// Clean up when the component is unmounted
useEffect(() => {
return () => {
if (timeout.current) clearTimeout(timeout.current);
};
}, []);
return (...args: Parameters<typeof fn>) => {
if (timeout.current) {
clearTimeout(timeout.current);
}
timeout.current = setTimeout(() => {
fn(...args);
timeout.current = undefined;
}, duration);
};
}