Files
Tom Moor 084490ba6b chore: Remove React in scope requirement (#9261)
* Add rules

* codemod: update-react-imports

* Update babelrc
2025-05-20 19:26:11 -04:00

33 lines
712 B
TypeScript

import { useRef, useEffect } from "react";
type Callback = () => void;
/**
* Hook to set up an interval that calls a callback.
*
* @param callback The callback to call.
* @param delay The delay in milliseconds.
*/
export default function useInterval(callback: Callback, delay: number) {
const savedCallback = useRef<Callback>();
// Remember the latest callback.
useEffect(() => {
savedCallback.current = callback;
}, [callback]);
// Set up the interval.
useEffect(() => {
function tick() {
savedCallback.current?.();
}
if (delay !== null) {
const id = setInterval(tick, delay);
return () => clearInterval(id);
}
return undefined;
}, [delay]);
}