vue 短信发送60s倒计时
时间: 2023-11-03 16:00:56 浏览: 89
简单实现vue验证码60秒倒计时功能
Vue短信发送60s倒计时可以通过Vue的响应式数据和计时器实现。在Vue中,可以使用computed属性来实时更新倒计时的文本,并在点击获取验证码按钮时启动计时器:
```javascript
export default {
data() {
return {
second: '', //倒计时
disabled: false, //是否禁用按钮
timer: null, //计时器
user: {
code: '' //验证码
}
};
},
computed: {
title() {
return this.disabled ? `重新获取 ( ${this.second} ) s` : '获取验证码';
},
},
methods: {
getCode() {
let that = this;
let s = 60; //倒计时间
if (!that.timer) {
that.second = s;
that.disabled = true;
that.timer = setInterval(() => {
if (that.second > 0 && this.second <= s) {
that.second--;
} else {
that.disabled = false;
clearInterval(that.timer);
this.timer = null;
}
}, 1000);
}
},
}
};
```
阅读全文