如何避免短时间重复点击导致的重复请求

1,012 阅读1分钟

加一个锁得概念,例子

var hasClick = false; //声明锁hasClick

Page({

  tap: function() {
//如果锁为true,则说明上一个请求还在处理中,则不发起新得请求
    if (hasClick) {

      return

    }

    hasClick = true

    wx.showLoading()



    wx.request({

      url: 'https://test.com/getinfo',

      method: 'POST',

      header: { 'content-type':'application/json' },

      data: { },

      success: function (res) {

        if (res.statusCode === 200) {

          console.log(res.data)// 服务器回包内容

        }

      },

      fail: function (res) {

        wx.showToast({ title: '系统错误' })

      },

      complete: function (res) {

        wx.hideLoading()

        hasClick = false

      }

    })

  }

})