原生微信小程序,手机号获取验证码倒计时

  data: {
    count: 60,
    mobile: '',
    code: '获取验证码',
  },
  //验证码
  textCode: function () {
    let that = this
    // 手机号码格式验证
    if (!(/^1[3456789]\d{9}$/.test(this.data.mobile))) {
      wx.showToast({
        title: '输入手机号有误',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (that.data.code !== '获取验证码') {
      return
    }
    const countDown = setInterval(() => {
      if (that.data.count <= 0) {
        that.setData({
          count: 60,
          code: '获取验证码'
        })
        clearInterval(countDown)
        return
      }
      that.data.count--
      that.setData({
        count: that.data.count,
        code: that.data.count < 10 ? `请等待0${that.data.count}s` : `请等待${that.data.count}s`
      })
    }, 1000);
    // 调用验证码接口
    that.textCode_http()
  },
posted @ 2021-10-11 15:29  喜欢Tb  阅读(14)  评论(0)    收藏  举报  来源