| 123456789101112131415161718192021222324252627282930313233343536373839 |
- // composables/useSendCode.js
- import { ref, onUnmounted } from 'vue'
- export function useSendCode() {
- const disabled = ref(false)
- const text = ref('获取验证码')
- let timer = null
- let count = 60
- const sendCode = () => {
- if (disabled.value) return
- disabled.value = true
- count = 60
- text.value = `剩余 ${count}s`
- timer = setInterval(() => {
- count--
- if (count < 0) {
- clearInterval(timer)
- disabled.value = false
- text.value = '重新获取'
- return
- }
- text.value = `剩余 ${count}s`
- }, 1000)
- }
- onUnmounted(() => {
- if (timer) clearInterval(timer)
- })
- return {
- disabled,
- text,
- sendCode
- }
- }
|