页面:
<view class="newaddrightBox msgBOXR">
<view class="getMsgBtn" bindtap="getMsgBtn" wx:if="{{isGetMsg}}">获取验证码</view>
<view class="getMsgBtn grayColor" wx:if="{{!isGetMsg}}">{{dTime}}s后重新获取</view>
</view>
js:
data: {
isGetMsg: true,
dTime: 60,
timer: ''
},
// 获取验证码
getMsgBtn () {
let that = this
// 判断是否填了手机号码,假设未填写
let hasPhoneNumber = false
if (hasPhoneNumber) {
that.setData({
isGetMsg: false,
dTime: 60,
timer: setInterval(function () {
let seconds = that.data.dTime
that.setData({ dTime: seconds - 1 })
if (that.data.dTime == 0) {
// 读秒结束 清空计时器
that.setData({
isGetMsg: true,
dTime: 60
})
clearInterval(that.data.timer)
}
}, 1000)
})
} else {
// 此为自定义的提示
this.tips.showTips('请填写手机号')
return;
}
},