提交代码
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
import { onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
|
||||
/** 秒级倒计时,归零时只触发一次回调。 */
|
||||
export function useCountdown(seconds: number, onComplete: () => void) {
|
||||
/** 秒级倒计时,归零时只触发一次回调;enabled 为 false 时不启动计时。 */
|
||||
export function useCountdown(
|
||||
seconds: number,
|
||||
onComplete: () => void,
|
||||
enabled = true,
|
||||
) {
|
||||
const remaining = ref(seconds)
|
||||
|
||||
let timer: number | undefined
|
||||
let finished = false
|
||||
|
||||
onMounted(() => {
|
||||
if (!enabled) return
|
||||
|
||||
timer = window.setInterval(() => {
|
||||
remaining.value = Math.max(0, remaining.value - 1)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user