lbry-desktop/ui/effects/use-interval.js
2022-03-02 20:20:27 -08:00

19 lines
422 B
JavaScript

import { useRef, useEffect } from 'react';
export default function useInterval(callback, delay) {
const savedCallback = useRef();
useEffect(() => {
savedCallback.current = callback;
}, [callback]);
useEffect(() => {
const tick = () => {
savedCallback.current();
};
if (delay !== null) {
let id = setInterval(tick, delay);
return () => clearInterval(id);
}
}, [delay]);
}