mirror of
https://github.com/router-for-me/Cli-Proxy-API-Management-Center.git
synced 2026-02-03 03:10:50 +08:00
25 lines
495 B
TypeScript
25 lines
495 B
TypeScript
/**
|
|
* 定时器 Hook
|
|
*/
|
|
|
|
import { useEffect, useRef } from 'react';
|
|
|
|
export function useInterval(callback: () => void, delay: number | null) {
|
|
const savedCallback = useRef<(() => void) | null>(null);
|
|
|
|
useEffect(() => {
|
|
savedCallback.current = callback;
|
|
}, [callback]);
|
|
|
|
useEffect(() => {
|
|
if (delay === null) return;
|
|
|
|
const tick = () => {
|
|
savedCallback.current?.();
|
|
};
|
|
|
|
const id = setInterval(tick, delay);
|
|
return () => clearInterval(id);
|
|
}, [delay]);
|
|
}
|