feat 搭建架构骨架
This commit is contained in:
32
src/composables/useCountDown.ts
Normal file
32
src/composables/useCountDown.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { computed, onScopeDispose, ref } from 'vue'
|
||||
|
||||
/** 倒计时(验证码 60s、二维码失效 4:32 等通用) */
|
||||
export function useCountDown(defaultSeconds = 60) {
|
||||
const seconds = ref(0)
|
||||
let timer: ReturnType<typeof setInterval> | null = null
|
||||
|
||||
const isCounting = computed(() => seconds.value > 0)
|
||||
|
||||
function start(total: number = defaultSeconds): void {
|
||||
reset()
|
||||
seconds.value = total
|
||||
timer = setInterval(() => {
|
||||
seconds.value -= 1
|
||||
if (seconds.value <= 0) {
|
||||
reset()
|
||||
}
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
function reset(): void {
|
||||
if (timer !== null) {
|
||||
clearInterval(timer)
|
||||
timer = null
|
||||
}
|
||||
seconds.value = 0
|
||||
}
|
||||
|
||||
onScopeDispose(reset)
|
||||
|
||||
return { seconds, isCounting, start, reset }
|
||||
}
|
||||
Reference in New Issue
Block a user