You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1018 B
44 lines
1018 B
import { useRef, useCallback, useEffect } from 'react'; |
|
|
|
export const useTimeout = () => { |
|
const timeoutRef = useRef<ReturnType<typeof setTimeout>>(); |
|
const callbackRef = useRef<() => void>(); |
|
|
|
const set = useCallback((callback: () => void, delay: number) => { |
|
if (timeoutRef.current) { |
|
clearTimeout(timeoutRef.current); |
|
} |
|
|
|
callbackRef.current = callback; |
|
timeoutRef.current = setTimeout(callback, delay); |
|
}, []); |
|
|
|
const delay = useCallback((delay: number) => { |
|
if (timeoutRef.current) { |
|
clearTimeout(timeoutRef.current); |
|
} |
|
|
|
if (!callbackRef.current) { |
|
return; |
|
} |
|
|
|
timeoutRef.current = setTimeout(callbackRef.current, delay); |
|
}, []); |
|
|
|
const cancel = useCallback(() => { |
|
if (timeoutRef.current) { |
|
clearTimeout(timeoutRef.current); |
|
timeoutRef.current = undefined; |
|
callbackRef.current = undefined; |
|
} |
|
}, []); |
|
|
|
useEffect( |
|
() => () => { |
|
cancel(); |
|
}, |
|
[cancel], |
|
); |
|
|
|
return [set, cancel, delay] as const; |
|
};
|
|
|