12345678910111213141516171819202122232425262728293031323334353637383940 |
- import { useEffect } from 'react';
- import useCommittedRef from './useCommittedRef';
- /**
- * Creates a `setInterval` that is properly cleaned up when a component unmounted
- *
- * @param fn an function run on each interval
- * @param ms The milliseconds duration of the interval
- */
-
- function useInterval(fn, ms, paused) {
- if (paused === void 0) {
- paused = false;
- }
-
- var handle;
- var fnRef = useCommittedRef(fn); // this ref is necessary b/c useEffect will sometimes miss a paused toggle
- // orphaning a setTimeout chain in the aether, so relying on it's refresh logic is not reliable.
-
- var pausedRef = useCommittedRef(paused);
-
- var tick = function tick() {
- if (pausedRef.current) return;
- fnRef.current();
- schedule(); // eslint-disable-line no-use-before-define
- };
-
- var schedule = function schedule() {
- clearTimeout(handle);
- handle = setTimeout(tick, ms);
- };
-
- useEffect(function () {
- schedule();
- return function () {
- return clearTimeout(handle);
- };
- }, [paused]);
- }
-
- export default useInterval;
|