按钮标签:
<el-button
type="primary"
@click="getVerify"
style="width:175px;"
:disabled="disabled=!show"
>
<span v-show="show">获取验证码</span>
<span v-show="!show" class="count">{{count}} s</span>
</el-button>
script:
<script>
export default {
data() {
return {
show: true,
count: "", // 初始化次数
timer: null
}
},
methods: {
getVerify() {
// 验证手机号
if (this.checkPhone() == false) {
return false;
} else {
const TIME_COUNT = 60; //更改倒计时时间
if (!this.timer) {
this.count = TIME_COUNT;
this.show = false;
this.timer = setInterval(() => {
if (this.count > 0 && this.count <= TIME_COUNT) {
this.count--;
} else {
this.show = true;
clearInterval(this.timer); // 清除定时器
this.timer = null;
}
}, 1000);
}
}
},
checkPhone() {
let phone = this.form.phone;
if (!/^1[3456789]\d{9}$/.test(phone)) {
this.$message.error("请填写正确的手机号");
return false;
}
}
}
}
</script>