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