mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-17 05:09:52 +03:00
translations and finish writeDevice
This commit is contained in:
22
interface/src/utils/useInterval.ts
Normal file
22
interface/src/utils/useInterval.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
// adapted from https://www.joshwcomeau.com/snippets/react-hooks/use-interval/
|
||||
export const useInterval = (callback: () => void, delay: number) => {
|
||||
const intervalRef = useRef<number | null>(null);
|
||||
const savedCallback = useRef<() => void>(callback);
|
||||
useEffect(() => {
|
||||
savedCallback.current = callback;
|
||||
}, [callback]);
|
||||
useEffect(() => {
|
||||
const tick = () => savedCallback.current();
|
||||
if (typeof delay === 'number') {
|
||||
intervalRef.current = window.setInterval(tick, delay);
|
||||
return () => {
|
||||
if (intervalRef.current !== null) {
|
||||
window.clearInterval(intervalRef.current);
|
||||
}
|
||||
};
|
||||
}
|
||||
}, [delay]);
|
||||
return intervalRef;
|
||||
};
|
||||
Reference in New Issue
Block a user