微信小程序手机验证码获取倒计时 刷新页面也会继续

wx.ml

  <view class="item {{change2?‘change_border‘:‘‘}}">
        <label for="">验证码</label>
        <input type="testmsg"  value="{{testmsg}}" data-index="2" bindblur="nochanegBorder" bindinput="changeValue" bindfocus="changeBorder" placeholder="请输入密码" />
        <view class="test {{getTest?‘‘:‘test_active‘}}" bindtap="{{getTest?‘‘:‘getTestMsg‘">{{getTest?‘重新获取‘+waitTime/1000+‘S‘:‘获取验证码‘}}</view>
    </view>

wx.js

  onLoad: function (options) {
    let that = this;
    let waitTime = wx.getStorageSync(‘waitTime‘)
    let getTest = wx.getStorageSync(‘getTest‘)
    if (waitTime) {
      that.setData({
        waitTime
      },()=>{
        if(waitTime!==0){
          that.getTestMsg()
        }
      })
    } else {
      wx.setStorageSync(‘waitTime‘, 60000)
    }
    if (getTest) {
      that.setData({
        getTest
      })
    } else {
      wx.setStorageSync(‘getTest‘, false)
    }
  },

  getTestMsg() {
    const that = this;
    let waitTime = wx.getStorageSync(‘waitTime‘);
    console.log(waitTime)
    if (!that.data.getTest) {
      that.setData({
        getTest: true
      },()=>{
        wx.setStorageSync(‘getTest‘,true)
      })
    }
    //发送验证码
    if (waitTime == 0) {
      that.setData({
        getTest: false
      }, () => {
        wx.setStorageSync(‘waitTime‘, 60000)
        wx.setStorageSync(‘getTest‘, false)
      })
      //重新获取验证码
    } else {
      setTimeout(() => {
        waitTime = waitTime - 1000;
        that.setData({
          waitTime
        }, () => {
          wx.setStorageSync(‘waitTime‘, waitTime)
          return that.getTestMsg()
        })
      }, 1000)
    }
  }

 

微信小程序手机验证码获取倒计时 刷新页面也会继续

上一篇:乐理普及——音乐人常说的Key到底是什么


下一篇:微信内h5页面打开小程序【详细教程】