import * as React from 'react';
import { useEffect, useRef, useState } from 'react';

export function useTimeout(millis: number, initValue: boolean = true) {
   const [isVisible, setIsVisible] = useState(initValue);
   const timer: React.MutableRefObject<NodeJS.Timeout> = useRef();

   useEffect(() => {
      timer.current = setTimeout(() => {
         setIsVisible(!isVisible);
      }, millis);

      return () => timer.current && clearTimeout(timer.current);
   }, []);

   return isVisible;
}
