Skip to content

Instantly share code, notes, and snippets.

View Andrewnt219's full-sized avatar

Andrew Nguyen Andrewnt219

View GitHub Profile
function useInterval(callback, delay) {
const intervalRef = React.useRef(null);
const savedCallback = React.useRef(callback);
React.useEffect(() => {
savedCallback.current = callback;
});
React.useEffect(() => {
const tick = () => savedCallback.current();
if (typeof delay === 'number') {
intervalRef.current = window.setInterval(tick, delay);