uniswap-interface-uncensored/src/hooks/useInterval.ts

23 lines
513 B
TypeScript
Raw Normal View History

2020-05-08 21:15:47 +03:00
import { useEffect, useRef } from 'react'
export default function useInterval(callback: () => void, delay: null | number) {
const savedCallback = useRef<() => void>()
// Remember the latest callback.
useEffect(() => {
savedCallback.current = callback
}, [callback])
// Set up the interval.
useEffect(() => {
function tick() {
savedCallback.current()
}
if (delay !== null) {
const id = setInterval(tick, delay)
2020-05-08 21:15:47 +03:00
return () => clearInterval(id)
}
}, [delay])
}