import React, { useState, useEffect, useRef } from 'react';
export default function useInterval(callback, delay) {
  var savedCallback = useRef(); // 保存新回调

  useEffect(function () {
    if (!savedCallback.current) {
      callback();
    }

    savedCallback.current = callback;
  }); // 建立 interval

  useEffect(function () {
    function tick() {
      savedCallback.current();
    }

    if (delay) {
      // eslint-disable-next-line prefer-const
      var id = setInterval(tick, delay);
      return function () {
        return clearInterval(id);
      };
    }
  }, [delay]);
}