mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-07 16:33:51 +00:00
2c47d90f34
* chore(lint): configure project-wide linter * chore(lint): improve linting in files * chore(lint): hide warnings in CI
28 lines
568 B
JavaScript
28 lines
568 B
JavaScript
import { useEffect, useRef } from 'react';
|
|
|
|
/**
|
|
* @description utility hook to around setInterval
|
|
* @param callback
|
|
* @param delay
|
|
*/
|
|
export const useInterval = (callback, delay) => {
|
|
const savedCallback = useRef();
|
|
|
|
useEffect(() => {
|
|
savedCallback.current = callback;
|
|
}, [callback]);
|
|
|
|
useEffect(() => {
|
|
/**
|
|
* @description function to be called
|
|
*/
|
|
function tick() {
|
|
savedCallback.current();
|
|
}
|
|
if (delay !== null) {
|
|
const id = setInterval(tick, delay);
|
|
return () => clearInterval(id);
|
|
}
|
|
}, [delay]);
|
|
};
|