budge/frontend/src/utils/useInterval.js
2022-02-07 19:16:47 -05:00

23 lines
484 B
JavaScript

import { useEffect, useRef } from 'react'
export default function (callback, delay) {
const savedCallback = useRef()
// Remember the last callbac
useEffect(() => {
savedCallback.current = callback
}, [callback])
// Set up the interval
useEffect(() => {
function tick() {
savedCallback.current()
}
if (delay !== null) {
const id = setInterval(tick, delay)
return () => {
clearInterval(id)
}
}
}, [callback, delay])
}