方法:用三目运算符
if为true即captchaTime为0时,可以获取验证码
点击获取验证码后用计时器倒计时,同时动态改变captchaTime的值,显示计时效果。
如果倒计时还未结束,用户点击则提示,不能重复获取
<view class="sendCaptcha">
<view class="verticalLine"></view>
<view class="cap-text" @click="sendCaptcha">{{!captchaTime?'获取验证码':captchaTime+'s'}}</view>
</view>
data(){
return {
phoneNumber:'',
captcha:'',
captchaTime:0,
}
},
/*
验证码倒计时
*/
function captchaCountdown(){
if(this.captchaTime > 0){
uni.showToast({
title:'不能重复获取',
icon:'none'
})
return ;
}else {
this.captchaTime = 60;
let timer = setInterval(()=>{
this.captchaTime--;
console.log(this.captchaTime)
if(this.captchaTime < 1){
clearInterval(timer);
this.captchaTime = 0
}
},1000)
}
}