bbgo_origin/frontend/hooks/useInterval.ts

20 lines
454 B
TypeScript
Raw Normal View History

2022-06-15 03:33:21 +00:00
import {useEffect, useRef} from "react";
export default function useInterval(cb: Function, delayMs: number | null) {
const savedCallback = useRef<Function>();
useEffect(() => {
savedCallback.current = cb
}, [cb])
useEffect(() => {
function tick() {
savedCallback.current();
}
if (delayMs !== null) {
let timerId = setInterval(tick, delayMs)
return () => clearInterval(timerId)
}
}, [delayMs])
}