util.js

小程序防止重复点击 随笔 第1张
// 防抖节流 防止重复点击
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

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。 小程序防止重复点击 随笔 第3张
//开头
const util = require('../../utils/util.js')


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

  }, 1000),
View Code

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄