File tree Expand file tree Collapse file tree 6 files changed +74
-25
lines changed
packages/hooks/src/useInterval Expand file tree Collapse file tree 6 files changed +74
-25
lines changed Original file line number Diff line number Diff line change 32
32
- name : Build Lib
33
33
run : |
34
34
pnpm build
35
+ - name : Build Docs plugin
36
+ run : |
37
+ cd packages/vitepress/vitepress-demo-block
38
+ pnpm run build
35
39
- name : Build Docs
36
40
run : |
37
41
cd packages/hooks
Original file line number Diff line number Diff line change 4
4
"private" : true ,
5
5
"scripts" : {
6
6
"bootstrap" : " tsx scripts/bootstrap.ts" ,
7
- "docs:dev" :" tsx scripts/docs.ts dev" ,
8
- "docs:build" :" tsx scripts/docs.ts build" ,
9
- "docs:build-github" :" tsx scripts/gitPage.ts github" ,
10
- "docs:build-gitee" :" tsx scripts/gitPage.ts gitee" ,
7
+ "build:vitepress-demo-block" :" cd packages/vitepress/vitepress-demo-block && pnpm build" ,
8
+ "docs:dev" :" pnpm build:vitepress-demo-block && tsx scripts/docs.ts dev" ,
9
+ "docs:build" :" pnpm build:vitepress-demo-block && tsx scripts/docs.ts build" ,
10
+ "docs:build-github" :" pnpm build:vitepress-demo-block && tsx scripts/gitPage.ts github" ,
11
+ "docs:build-gitee" :" pnpm build:vitepress-demo-block && tsx scripts/gitPage.ts gitee" ,
11
12
"clean" : " rimraf dist lib es" ,
12
13
"build" : " pnpm bootstrap && tsx scripts/build.ts" ,
13
14
"test" : " vitest" ,
Original file line number Diff line number Diff line change 1
1
<template >
2
- <div >
3
- {{ valueRef }}
4
- </div >
2
+ <div > value: {{ valueRef }} </div >
3
+ <vhp-button style =" margin-top : 8px ;" @click =" handleInterval" >interval + 1000</vhp-button >
4
+ <vhp-button style =" margin-left : 8px ;" @click =" resetInterval" > reset interval</vhp-button >
5
+ <vhp-button style =" margin-left : 8px ;" @click =" clear" >clear</vhp-button >
6
+ <vhp-button style =" margin-left : 8px ;" @click =" restart" >restart</vhp-button >
5
7
</template >
6
8
7
9
<script lang="ts" setup>
8
- import { ref } from ' vue'
10
+ import { ref } from ' vue'
9
11
10
- import { useInterval } from ' vue-hooks-plus'
12
+ import { useInterval } from ' vue-hooks-plus'
11
13
12
- const valueRef = ref (0 )
13
- useInterval (() => {
14
- valueRef .value += 1
15
- }, 2000 )
14
+ const valueRef = ref (0 )
15
+ const intervalRef = ref (2000 )
16
+ const { clear, restart } = useInterval (() => {
17
+ valueRef .value += 1
18
+ }, intervalRef )
19
+
20
+ const handleInterval = () => {
21
+ intervalRef .value += 1000
22
+ }
23
+
24
+ const resetInterval = () => {
25
+ intervalRef .value = 2000
26
+ }
16
27
</script >
Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ useInterval(
41
41
42
42
## Result
43
43
44
- | Property | Description | Type |
45
- | ------------- | -------------- | ------------ |
46
- | clearInterval | clear interval | ` () => void ` |
44
+ | Property | Description | Type |
45
+ | -------- | ---------------- | ------------ |
46
+ | clear | clear interval | ` () => void ` |
47
+ | restart | restart interval | ` () => void ` |
Original file line number Diff line number Diff line change @@ -16,28 +16,59 @@ function useInterval(
16
16
*/
17
17
immediate ?: boolean
18
18
} ,
19
- ) : void {
19
+ ) : {
20
+ /**
21
+ * Clear the interval timer.
22
+ */
23
+ clear : VoidFunction
24
+ /**
25
+ * Restart the interval timer.
26
+ */
27
+ restart : VoidFunction
28
+ } {
20
29
const immediate = options ?. immediate
21
30
22
31
const fnRef = ref ( fn )
23
32
24
- watchEffect ( onInvalidate => {
33
+ const timerRef = ref < ReturnType < typeof setInterval > | null > ( null )
34
+
35
+ const setupInterval = ( ) => {
25
36
if ( isRef ( delay ) ) {
26
37
if ( typeof delay . value !== 'number' || delay . value < 0 ) return
27
38
} else {
28
39
if ( typeof delay !== 'number' || delay < 0 ) return
29
40
}
41
+
30
42
if ( immediate ) {
31
43
fnRef . value ( )
32
44
}
45
+
33
46
const _deply = unref ( delay )
34
- const timer = setInterval ( ( ) => {
47
+ timerRef . value = setInterval ( ( ) => {
35
48
fnRef . value ( )
36
49
} , _deply )
37
- onInvalidate ( ( ) => {
38
- clearInterval ( timer )
39
- } )
50
+ }
51
+
52
+ const clear = ( ) => {
53
+ if ( timerRef . value ) {
54
+ clearInterval ( timerRef . value )
55
+ }
56
+ }
57
+
58
+ const restart = ( ) => {
59
+ clear ( )
60
+ setupInterval ( )
61
+ }
62
+
63
+ watchEffect ( onInvalidate => {
64
+ setupInterval ( )
65
+ onInvalidate ( clear )
40
66
} )
67
+
68
+ return {
69
+ clear,
70
+ restart,
71
+ }
41
72
}
42
73
43
74
export default useInterval
Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ useInterval(
41
41
42
42
## Result
43
43
44
- | 参数 | 说明 | 类型 |
45
- | ------------- | ---------- | ------------ |
46
- | clearInterval | 清除定时器 | ` () => void ` |
44
+ | 参数 | 说明 | 类型 |
45
+ | ------- | -------------- | ------------ |
46
+ | clear | 清除定时器 | ` () => void ` |
47
+ | restart | 重新启动定时器 | ` () => void ` |
You can’t perform that action at this time.
0 commit comments