小程序防止重复点击

util.js

// 防抖节流 防止重复点击
function throttle(fn, gapTime) {
  if (gapTime == null || gapTime == undefined) {
    gapTime = 1500
  }

  let _lastTime = null

  // 返回新的函数
  return function () {
    let _nowTime = + new Date()
    if (_nowTime - _lastTime > gapTime || !_lastTime) {
      fn.apply(this, arguments)   //将this和参数传给原函数
      _lastTime = _nowTime
    }
  }
}


module.exports = {
  throttle: throttle
}
View Code

index.js

//开头
const util = require('../../utils/util.js')


//某个方法
  withdraw: util.throttle(function (e) {

  }, 1000),
View Code
原文地址:https://www.cnblogs.com/zwyboom/p/10763478.html