微信小程序 防止重复点击(节流函数)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/namechenfl/article/details/89211716

utils.js

//防止多次重复点击  (函数节流)
 function throttle(fn, gapTime) {
  if (gapTime == null || gapTime == undefined) {
    gapTime = 1000
  }

  let _lastTime = null

  // 返回新的函数
  return function (e) {
    console.log(this)
    let _nowTime = + new Date()
    if (_nowTime - _lastTime > gapTime || !_lastTime) {
      // fn.apply(this, arguments)   //将this和参数传给原函数
      fn(this,e)    //上方法不可行的解决办法 改变this和e
      _lastTime = _nowTime
    }
  }
}
module.exports = {
  throttle: throttle
}

页面js

//mine.js
bindUpload: utils.throttle((that,e) => {
   
    console.log(e)    //    事件源
    console.log(that)  // this 指向
  }, 1000)

猜你喜欢

转载自blog.csdn.net/namechenfl/article/details/89211716