import { useEffect, useMemo, useRef } from "react"; export default function useThrottle(callback: Function, wait: number) { const ref = useRef(undefined); useEffect(() => { ref.current = callback; }, [callback]); const throttledCallback = useMemo(() => { let lastCall = 0; return function(...args: any[]) { const now = Date.now(); if (now - lastCall >= wait) { lastCall = now; ref.current?.(...args); } }; }, [wait]); return throttledCallback; }