const useInterval = (callback, delay) => {
        const savedCallback = useRef();

        // 保存新回调
        useEffect(() => {
            savedCallback.current = callback;
        });

        // 建立 interval
        useEffect(() => {
            function tick() {
                savedCallback.current();
            }
            if (delay !== null) {
                let id = setInterval(tick, delay);
                return () => clearInterval(id);
            }
        }, [delay]);
    }

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2021-05-08
  • 2022-01-07
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
  • 2021-06-30
相关资源
相似解决方案