Skip to content

Commit 0ed9825

Browse files
committed
fix: use-interval no clear func
1 parent dfe9f26 commit 0ed9825

File tree

4 files changed

+65
-21
lines changed

4 files changed

+65
-21
lines changed
Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
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>

packages/hooks/src/useInterval/index.en-US.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff 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` |

packages/hooks/src/useInterval/index.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff 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

4374
export default useInterval

packages/hooks/src/useInterval/index.zh-CN.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ useInterval(
4141

4242
## Result
4343

44-
| 参数 | 说明 | 类型 |
45-
| ------------- | ---------- | ------------ |
46-
| clearInterval | 清除定时器 | `() => void` |
44+
| 参数 | 说明 | 类型 |
45+
| ------- | -------------- | ------------ |
46+
| clear | 清除定时器 | `() => void` |
47+
| restart | 重新启动定时器 | `() => void` |

0 commit comments

Comments
 (0)