@boomerang-io/utils
Version:
A library of reusable utilities and hooks for React webapps on the Boomerang platform.
21 lines (18 loc) • 516 B
JavaScript
import { useEffect, useRef } from "react";
export default function useInterval(callback, delay) {
var savedCallback = useRef(); // Remember the latest callback.
useEffect(function () {
savedCallback.current = callback;
}, [callback]); // Set up the interval.
useEffect(function () {
function tick() {
savedCallback.current();
}
if (delay !== null) {
var id = setInterval(tick, delay);
return function () {
return clearInterval(id);
};
}
}, [delay]);
}