mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-09 06:55:51 +01:00
23 lines
469 B
TypeScript
23 lines
469 B
TypeScript
import { useEffect, useRef } from 'react';
|
|
|
|
export function useSetSafeInterval() {
|
|
const timers = useRef<NodeJS.Timer[]>([]);
|
|
|
|
function setSafeInterval(callback: () => void, delay: number) {
|
|
const newInterval = setInterval(callback, delay);
|
|
timers.current.push(newInterval);
|
|
return newInterval;
|
|
}
|
|
|
|
useEffect(
|
|
() => () => {
|
|
timers.current.forEach((t) => {
|
|
clearInterval(t);
|
|
});
|
|
},
|
|
[]
|
|
);
|
|
|
|
return setSafeInterval;
|
|
}
|