小程序获取验证码倒计时功能

分享一段小程序倒计时的代码



Page({

  /**
   * 页面的初始数据
   */
  data: {
    timeCountDownTop: "获取验证码",
    second: 90,
    counting: false
  },

  getVerifityCode: function() {
    var that = this;
    if (!that.data.counting) {
      wx.showToast({
        title: '验证码已发送',
      })
      //开始倒计时
      countDown(that, that.data.second);
    }

    function countDown(that, count) {
      if (count == 0) {
        that.setData({
          timeCountDownTop: '获取验证码',
          counting: false
        })
        return;
      }

      that.setData({
        counting: true,
        timeCountDownTop: count + '秒后重新获取',
        second: count
      })
      setTimeout(function() {
        count--;
        countDown(that, count);
      }, 1000);
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
})
发布了102 篇原创文章 · 获赞 26 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/xuelian3015/article/details/98880333