forked from vectorisvector/Polaris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add useInterval hook and SendTransactionErrorType import
- Loading branch information
1 parent
2089b22
commit 48c0efd
Showing
3 changed files
with
93 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
import { sleep } from "@/utils/helper"; | ||
|
||
export default function useInterval( | ||
callback: () => Promise<void>, | ||
delay: number | null, | ||
) { | ||
const savedCallback = useRef(callback); | ||
|
||
useEffect(() => { | ||
savedCallback.current = callback; | ||
}, [callback]); | ||
|
||
// 设置和清除循环 | ||
useEffect(() => { | ||
let isRunning = true; | ||
|
||
async function tick() { | ||
while (isRunning && delay !== null) { | ||
await savedCallback.current(); | ||
await sleep(delay); | ||
} | ||
} | ||
|
||
tick(); | ||
|
||
return () => { | ||
isRunning = false; | ||
}; | ||
}, [delay]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters