mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
frontend: useInterval hook
This commit is contained in:
parent
b2ba0a86d8
commit
276319c1f3
20
frontend/hooks/useInterval.ts
Normal file
20
frontend/hooks/useInterval.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
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])
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user