每次改變數據的時候記得調用 this.$apply()
驗證碼倒計時
使用的vant-weapp UI組件
wxml:
1 <van-col span="10" style='text-align:center'> 3 <view wx:if="{{sendAuthCode}}"bindtap="getCode()">獲取驗證碼</view> 6 <view wx:elif="{{!sendAuthCode}}">{{auth_time}}S</view> 8 </van-col>
JS:
1 data = { 2 auth_time: 5,//倒計時 3 sendAuthCode: true,//控制獲取驗證碼 4 }; 5 methods = { 6 getCode () {//獲取驗證碼 7 let that = this 8 this.sendAuthCode = false 9 var auth_timetimer = setInterval(() => { 10 this.auth_time--; 11 that.$apply() 12 console.log(this.auth_time) 13 if (this.auth_time <= 0) { 14 this.sendAuthCode = true; 15 that.$apply() 16 clearInterval(auth_timetimer); 17 } 18 }, 1000) 19 } 20 }