@@ -8,18 +8,32 @@ import useCommittedRef from './useCommittedRef'
8
8
* @param ms The milliseconds duration of the interval
9
9
*/
10
10
function useInterval ( fn : ( ) => void , ms : number ) : void
11
+
11
12
/**
12
- * Creates a pasuable `setInterval` that is properly cleaned up when a component unmounted
13
+ * Creates a pausable `setInterval` that is properly cleaned up when a component unmounted
13
14
*
14
15
* @param fn an function run on each interval
15
16
* @param ms The milliseconds duration of the interval
16
17
* @param paused Whether or not the interval is currently running
17
18
*/
18
19
function useInterval ( fn : ( ) => void , ms : number , paused : boolean ) : void
20
+
21
+ /**
22
+ * Creates a pausable `setInterval` that is properly cleaned up when a component unmounted
23
+ *
24
+ * @param fn an function run on each interval
25
+ * @param ms The milliseconds duration of the interval
26
+ * @param paused Whether or not the interval is currently running
27
+ * @param runImmediately Whether to run the function immediately on mount or unpause
28
+ * rather than waiting for the first interval to elapse
29
+ */
30
+ function useInterval ( fn : ( ) => void , ms : number , paused : boolean , runImmediately : boolean ) : void
31
+
19
32
function useInterval (
20
33
fn : ( ) => void ,
21
34
ms : number ,
22
35
paused : boolean = false ,
36
+ runImmediately : boolean = false ,
23
37
) : void {
24
38
let handle : number
25
39
const fnRef = useCommittedRef ( fn )
@@ -38,9 +52,13 @@ function useInterval(
38
52
}
39
53
40
54
useEffect ( ( ) => {
41
- schedule ( )
55
+ if ( runImmediately ) {
56
+ tick ( )
57
+ } else {
58
+ schedule ( )
59
+ }
42
60
return ( ) => clearTimeout ( handle )
43
- } , [ paused ] )
61
+ } , [ paused , runImmediately ] )
44
62
}
45
63
46
64
export default useInterval
0 commit comments