mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-15 04:10:01 +03:00
26 lines
716 B
TypeScript
26 lines
716 B
TypeScript
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 = DEFAULT_DELAY) => {
|
|
const intervalRef = useRef<number | null>(null);
|
|
const savedCallback = useRef<() => void>(callback);
|
|
|
|
useEffect(() => {
|
|
savedCallback.current = callback;
|
|
}, [callback]);
|
|
|
|
useEffect(() => {
|
|
const tick = () => savedCallback.current();
|
|
intervalRef.current = window.setInterval(tick, delay);
|
|
return () => {
|
|
if (intervalRef.current !== null) {
|
|
window.clearInterval(intervalRef.current);
|
|
}
|
|
};
|
|
}, [delay]);
|
|
|
|
return intervalRef;
|
|
};
|