小程序设置验证码倒计时

155 阅读1分钟

小程序设置一个验证码倒计时,直接复制粘贴即可 新建了个群,欢迎大家一起进群讨论459358760

<button class="sends" 
bindtap="getVerificationCode" disabled='{{disabled}}'>{{time}}</button>

// pages/forget/forget.js
var interval = null //倒计时函数
Page({

    /**
     * 页面的初始数据
     */
      data: {
        time: '获取验证码', //倒计时 
        currentTime:60 , //定时器时间
      }, 
      getCode: function (options){
        var that = this;
        var currentTime = that.data.currentTime
        interval = setInterval(function () {
          currentTime--;
          that.setData({
            time: currentTime+'秒'
          })
          if (currentTime <= 0) { //倒计时等于0时解除定时器
            clearInterval(interval)//接除定时器
            that.setData({
              time: '重新发送',
              currentTime:61,
              disabled: false  //解除按钮禁用 
            })
          }
        }, 1000)//设置多久触发一次
      },
      getVerificationCode(){
        this.getCode();
        var that = this
        that.setData({
          disabled:true
        })
      },
})