import { useCallback, useEffect, useRef } from "react"; type TimeoutHookReturn = { reset: () => void; clear: () => void; }; export default function useTimeout( callback: () => void, delay: number ): TimeoutHookReturn { const callbackRef = useRef<() => void>(callback); const timeoutRef = useRef(); useEffect(() => { callbackRef.current = callback; }, [callback]); const set = useCallback(() => { timeoutRef.current = window.setTimeout(() => callbackRef.current(), delay); }, [delay]); const clear = useCallback(() => { timeoutRef.current && clearTimeout(timeoutRef.current); }, []); useEffect(() => { set(); return clear; }, [delay, set, clear]); const reset = useCallback(() => { clear(); set(); }, [clear, set]); return { reset, clear }; }