import { useEffect, useRef } from 'react';

export const useInterval = (callback: Function, delay: number = 1000) => {
  const savedCallback = useRef(null);

  useEffect(() => {
    savedCallback.current = callback;
  }, [callback]);

  useEffect(() => {
    if (delay !== null) {
      const id = setInterval(() => {
        savedCallback.current(id);
      }, delay);

      return () => clearInterval(id);
    }
  }, [delay]);
};

export default useInterval;
