如图所示,在span标签里通过count显示数据,实现倒计时功能,倒计时结束,span标签隐藏
<span v-show="!showButton">{{count}}s</span>
<el-button @click="downTime()">点击倒计时</el-button>
定义数据
data() {
return {
count:'',
showButton:true,
timer: null,
}
},
downTime(){
const countDown = 30; //定义倒计时时间
if (!this.timer) {
this.count = countDown ;
this.showButton = false;
this.timer = setInterval(() => {
if (this.count > 0 && this.count <= countDown ) {
this.count--;
} else {
this.showButton = true;
clearInterval(this.timer); //clearInterval()调用 setInterval() 函数获得的返回值取消该setInterval()所设定的定时执行操作。
this.timer = null;
}
}, 1000)
}
},