按钮倒计时结束恢复点击功能,用于页面中协议知晓倒计时等
以app为例,效果图:
页面代码:
<view>
<button :disabled="countdown > 0" @click="agree" type="">{
{ countdown > 0 ? `我已阅读${countdown}秒` : '同意' }}</button>
</view>
data声明:
data() {
return {
countdown: 0, // 初始倒计时时间
timer: null, // 计时器
}
},
倒计时代码:
methods: {
// 倒计时
time() {
this.countdown = 10;
this.timer = setInterval(() => {
if (this.countdown > 0) {
this.countdown -= 1;
} else {
clearInterval(this.timer);
this.timer = null;
}
}, 1000);
},
}
可以在刚进入页面onShow()的时候触发一下time()倒计时方法,或者根据具体需要的业务场景触发