Something went wrong. Try again.
Starship Simulator Controls thoriumsim.com
Something went wrong. Try again.
509 B · 21 lines
TypeScript
at develop
12345678910111213141516171819202122import { useEffect, useRef } from "react";
export default function useInterval(callback: () => void, delay: number | null = 1000) { const savedCallback = useRef<() => void>(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); } }, [delay]);}