const useInterval = (handler: () => void, timeout: number) => {
const intervalHandler = useRef<number>();
const handleClearInterval = useCallback(() => {
if (intervalHandler.current) {
clearInterval(intervalHandler.current);
}
}, []);
useEffect(() => {
handleClearInterval();
intervalHandler.current = setInterval(handler, timeout);
return handleClearInterval;
}, [handleClearInterval, handler, timeout]);
return { handleClearInterval };
};