show ntp time in status, use useInterval everywhere instead of alova's AutoRequest

This commit is contained in:
proddy
2024-12-21 16:30:05 +01:00
parent 6d22f6aebf
commit b0c29b57c7
17 changed files with 80 additions and 63 deletions

View File

@@ -1,7 +1,9 @@
import { useEffect, useRef } from 'react';
const DEFAULT_DELAY = 3000;
// adapted from https://www.joshwcomeau.com/snippets/react-hooks/use-interval/
export const useInterval = (callback: () => void, delay: number) => {
export const useInterval = (callback: () => void, delay: number = DEFAULT_DELAY) => {
const intervalRef = useRef<number | null>(null);
const savedCallback = useRef<() => void>(callback);
@@ -11,14 +13,12 @@ export const useInterval = (callback: () => void, delay: number) => {
useEffect(() => {
const tick = () => savedCallback.current();
if (typeof delay === 'number') {
intervalRef.current = window.setInterval(tick, delay);
return () => {
if (intervalRef.current !== null) {
window.clearInterval(intervalRef.current);
}
};
}
intervalRef.current = window.setInterval(tick, delay);
return () => {
if (intervalRef.current !== null) {
window.clearInterval(intervalRef.current);
}
};
}, [delay]);
return intervalRef;