微信小程序-短信验证码倒计时简单实现

话不多说,直接上代码:

var countdown = 60;// 倒计时60s定义

var settime = function (that) {

  if (countdown == 0) {

    that.setData({

      alreadyGetVerifyCode: false

    })

    countdown = 60;

    return;

  } else {

    that.setData({

      alreadyGetVerifyCode: true,

      coolingTime: `${countdown}s重新获取`

    })

    countdown--;

  }

  setTimeout(function () {

    settime(that)

  }

    , 1000)

}

Page({

  /**

  * 页面的初始数据

  */

  data: {

    alreadyGetVerifyCode:false,

    coolingTime:''

  },

/**

  * 获取验证码事件

  */

  getVerifyCodeAction:function(e) {

    if (!this.data.alreadyGetVerifyCode) {

      var that = this;

      that.setData({

        alreadyGetVerifyCode: (!that.data.alreadyGetVerifyCode)

      })

      settime(that);

    } else {

      console.log('验证码CD');

    }

  }

你可能感兴趣的:(微信小程序-短信验证码倒计时简单实现)