|
@@ -3,14 +3,18 @@ import { useEffect, useRef } from "react";
|
3
|
3
|
/**
|
4
|
4
|
* 计时器
|
5
|
5
|
*/
|
6
|
|
-export default function useInterval(fn, delay) {
|
|
6
|
+export default function useInterval(fn, delay, immediate) {
|
7
|
7
|
const callbackRef = useRef()
|
8
|
8
|
callbackRef.current = fn;
|
9
|
9
|
|
10
|
10
|
useEffect(() => {
|
11
|
11
|
if (delay === undefined || delay === null) return;
|
12
|
12
|
|
|
13
|
+ if (immediate) {
|
|
14
|
+ callbackRef.current()
|
|
15
|
+ }
|
|
16
|
+
|
13
|
17
|
const ticker = setInterval(() => callbackRef.current(), delay)
|
14
|
18
|
return () => clearInterval(ticker)
|
15
|
|
- }, [delay])
|
|
19
|
+ }, [delay, immediate])
|
16
|
20
|
}
|