UNPKG

reactuals

Version:

A useful package providing a collection of 50+ React hooks and utilities to simplify React development.

19 lines (18 loc) 562 B
import { useEffect, useRef } from "react"; /** * Runs a function repeatedly at a set interval. * @param callback - Function to call * @param delay - Delay in ms (set null to pause) */ export function useInterval(callback, delay) { const savedCallback = useRef(callback); useEffect(() => { savedCallback.current = callback; }, [callback]); useEffect(() => { if (delay === null) return; const id = setInterval(() => savedCallback.current(), delay); return () => clearInterval(id); }, [delay]); }