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 3232 - name : Build Lib
3333 run : |
3434 pnpm build
35+ - name : Build Docs plugin
36+ run : |
37+ cd packages/vitepress/vitepress-demo-block
38+ pnpm run build
3539 - name : Build Docs
3640 run : |
3741 cd packages/hooks
Original file line number Diff line number Diff line change 44 "private" : true ,
55 "scripts" : {
66 "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" ,
1112 "clean" : " rimraf dist lib es" ,
1213 "build" : " pnpm bootstrap && tsx scripts/build.ts" ,
1314 "test" : " vitest" ,
Original file line number Diff line number Diff line change 11<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 >
57</template >
68
79<script lang="ts" setup>
8- import { ref } from ' vue'
10+ import { ref } from ' vue'
911
10- import { useInterval } from ' vue-hooks-plus'
12+ import { useInterval } from ' vue-hooks-plus'
1113
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+ }
1627 </script >
Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ useInterval(
4141
4242## Result
4343
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(
1616 */
1717 immediate ?: boolean
1818 } ,
19- ) : void {
19+ ) : {
20+ /**
21+ * Clear the interval timer.
22+ */
23+ clear : VoidFunction
24+ /**
25+ * Restart the interval timer.
26+ */
27+ restart : VoidFunction
28+ } {
2029 const immediate = options ?. immediate
2130
2231 const fnRef = ref ( fn )
2332
24- watchEffect ( onInvalidate => {
33+ const timerRef = ref < ReturnType < typeof setInterval > | null > ( null )
34+
35+ const setupInterval = ( ) => {
2536 if ( isRef ( delay ) ) {
2637 if ( typeof delay . value !== 'number' || delay . value < 0 ) return
2738 } else {
2839 if ( typeof delay !== 'number' || delay < 0 ) return
2940 }
41+
3042 if ( immediate ) {
3143 fnRef . value ( )
3244 }
45+
3346 const _deply = unref ( delay )
34- const timer = setInterval ( ( ) => {
47+ timerRef . value = setInterval ( ( ) => {
3548 fnRef . value ( )
3649 } , _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 )
4066 } )
67+
68+ return {
69+ clear,
70+ restart,
71+ }
4172}
4273
4374export default useInterval
Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ useInterval(
4141
4242## Result
4343
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