1 import { useEffect, useState } from 'react';
4 refreshInterval: number;
7 const useHasSuspendedCounter = ({ refreshInterval, tolerance }: Props) => {
8 const [counter, setCounter] = useState(0);
11 let previousTime = Date.now();
12 let unmounted = false;
14 const intervalHandle = window.setInterval(() => {
15 const currentTime = Date.now();
16 const computedInterval = currentTime - previousTime;
17 const isOutOfSync = Math.abs(computedInterval - refreshInterval) > tolerance;
18 if (isOutOfSync && !unmounted) {
19 setCounter((counter) => counter + 1);
21 previousTime = currentTime;
26 clearInterval(intervalHandle);
28 }, [refreshInterval, tolerance]);
33 export default useHasSuspendedCounter;