( callback: () => void, intervalMs: number | null, )
| 41 | * one wake-up. Pass `null` for intervalMs to pause. |
| 42 | */ |
| 43 | export function useInterval( |
| 44 | callback: () => void, |
| 45 | intervalMs: number | null, |
| 46 | ): void { |
| 47 | const callbackRef = useRef(callback) |
| 48 | callbackRef.current = callback |
| 49 | |
| 50 | const clock = useContext(ClockContext) |
| 51 | |
| 52 | useEffect(() => { |
| 53 | if (!clock || intervalMs === null) return |
| 54 | |
| 55 | let lastUpdate = clock.now() |
| 56 | |
| 57 | const onChange = (): void => { |
| 58 | const now = clock.now() |
| 59 | if (now - lastUpdate >= intervalMs) { |
| 60 | lastUpdate = now |
| 61 | callbackRef.current() |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return clock.subscribe(onChange, false) |
| 66 | }, [clock, intervalMs]) |
| 67 | } |
| 68 |
no outgoing calls
no test coverage detected