UNPKG

slanted-gamedev-toolz

Version:

A slanted mix of tools for your brilliant ideas in game design.

21 lines (20 loc) 619 B
import { useEffect, useRef } from "react"; export default function useCatInterval(callback, delay) { const savedCallback = useRef(null); // Remember the latest callback. useEffect(() => { savedCallback.current = callback; }, [callback]); // Set up the interval. useEffect(() => { function tick() { if (savedCallback.current) { savedCallback.current(); } } if (delay !== null && delay !== undefined) { const id = setInterval(tick, delay); return () => clearInterval(id); } }, [delay]); }