lbry-desktop/ui/effects/use-interval.js

20 lines
424 B
JavaScript
Raw Normal View History

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) {
2022-03-03 03:25:44 +01:00
const id = setInterval(tick, delay);
return () => clearInterval(id);
}
}, [delay]);
}