html代码
<el-button style="width:52%;padding:12px 0px" :class="{disabled: !this.canClick}" class="button" @click="countDown">
{{content}}
</el-button>
data中定义参数
content: '发送验证码', // 按钮里显示的内容
totalTime: 60 , //记录具体倒计时时间
canClick: true //添加canClick
js中代码
//获取验证码
countDown() {
if (!this.canClick) return //改动的是这两行代码
this.canClick = false
this.content = this.totalTime + 's后重新发送'
let clock = window.setInterval(() => {
this.totalTime--
this.content = this.totalTime + 's后重新发送'
if (this.totalTime < 0) {
window.clearInterval(clock)
this.content = '重新发送验证码'
this.totalTime = 60
this.canClick = true //这里重新开启
}
},1000)
}
可以添加一些css.大家可以根据自己的样式来改变
.disabled{
background-color: #ddd;
border-color: #ddd;
color:#57a3f3;
cursor: not-allowed;
}
到这里完整的倒计时就ok了,喜欢的给个小心心哦