微信小程序防止重複點擊

在/utils/util.js(工具類)加入以下代碼

module.exports = {
  throttle: throttle
}
// 函數節流(throttle):函數在一段時間內多次觸發只會執行第一次
const throttle = (fn, gapTime) => {
  if (gapTime == null || gapTime == undefined) {
    gapTime = 1500
  }
  let _lastTime = null
  return function () {
    let _nowTime = + new Date()
    if (_nowTime - _lastTime > gapTime || !_lastTime) {
      // 將this和參數傳給原函數
      fn.apply(this, arguments)
      _lastTime = _nowTime
    }
  }
}

在使用的頁面:在/index/index.js使用

toStoreList是click的事件名

 toStoreList: util.throttle(function () {
       console.log('111111111')
  },1000),

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章