You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
899 B

11 months ago
export function useSmsCode() {
// 数据
const disabled:globalThis.Ref<boolean> = ref<boolean>(false)
const text:globalThis.Ref<string> = ref<string>('获取验证码')
let timeoutId: any;
const handleCodeSend = (timer: number = 60) => {
if (disabled.value) return
disabled.value = true
let n: number = timer
text.value = '剩余 ' + n + ' s'
timeoutId = setInterval(() => {
n = n - 1
if (n < 0) {
clearInterval(timeoutId)
}
text.value = '剩余 ' + n + ' s'
if (n <= 0) {
disabled.value = false
text.value = '重新获取'
}
}, 1000)
}
//清除倒计时
const stopCountdown = ()=>{
disabled.value = false
if (timeoutId !== undefined) {
clearInterval(timeoutId); // 清除定时器
}
}
return {
disabled,
text,
handleCodeSend,
stopCountdown
}
}