UNPKG

@modern-kit/react

Version:
29 lines (26 loc) 747 B
import { useRef, useCallback, useEffect } from 'react'; const useManualTimeout = () => { const timeoutIds = useRef([]); const clearAll = useCallback(() => { for (let i = 0; i < timeoutIds.current.length; i++) { clearTimeout(timeoutIds.current[i]); } timeoutIds.current = []; }, []); const set = useCallback((callback, delay) => { const id = window.setTimeout(() => { timeoutIds.current = timeoutIds.current.filter( (timeoutId) => timeoutId !== id ); callback(); }, delay); timeoutIds.current.push(id); return id; }, []); useEffect(() => { return clearAll; }, [clearAll]); return { set, clearAll }; }; export { useManualTimeout }; //# sourceMappingURL=index.mjs.map