uniapp手机号验证码登录倒计时60秒代码
时间: 2023-06-11 21:07:41 浏览: 144
以下是一个简单的UniApp手机号验证码登录倒计时60秒的代码:
1. 在模板页面中添加以下代码:
```html
<template>
<view>
<input type="tel" v-model="phone" placeholder="请输入手机号" />
<input type="tel" v-model="verifyCode" placeholder="请输入验证码" />
<button @click="sendVerifyCode" :disabled="disabled">{{ disabled ? `${count}s后重试` : '获取验证码' }}</button>
</view>
</template>
```
2. 在script中添加以下代码:
```javascript
<script>
export default {
data() {
return {
phone: '', // 手机号码
verifyCode: '', // 验证码
count: 60, // 倒计时时间
timer: null, // 定时器
disabled: false // 是否禁用按钮
}
},
methods: {
// 发送验证码
sendVerifyCode() {
// 先判断手机号码是否正确
if (!/^[1][3,4,5,7,8][0-9]{9}$/.test(this.phone)) {
uni.showToast({
title: '请输入正确的手机号码',
icon: 'none'
})
return
}
// 启用倒计时
this.disabled = true
// 调用发送验证码的接口,这里只是模拟,实际开发中需要替换成你的接口
this.timer = setInterval(() => {
if (this.count <= 1) {
clearInterval(this.timer)
this.timer = null
this.count = 60
this.disabled = false
} else {
this.count--
}
}, 1000)
}
}
}
</script>
```
这段代码中,我们定义了一个 `count` 变量,表示倒计时的时间,初始值为60。当用户点击获取验证码按钮时,我们会先判断手机号码是否正确,如果正确则启动定时器,每秒减少 `count` 的值,直到 `count` 减少到0时,禁用按钮并清除定时器。在模板页面中,我们使用了 `:disabled` 来根据 `disabled` 变量的值来控制按钮是否禁用,并使用模板字符串来动态显示倒计时。