微信小程序防止多次点击,函数延迟执行

//util.js
// 延迟执行函数
// fn 需要延迟执行的函数
// gapTime  需要延迟的时间
function throttle(fn, gapTime) {
  if (gapTime == null || gapTime == undefined) {
      gapTime = 1000
  }
  let _lastTime = null
  // 返回新的函数
  return function () {
      let _nowTime = + new Date()
      if (_nowTime - _lastTime > gapTime || !_lastTime) {
          fn.apply(this, arguments)   //将this和参数传给原函数,不然e是'undefine'
          _lastTime = _nowTime
      }
  }
}
module.exports = {
  throttle,
}

例子:

var utils = require('../../../utils/util');

btnClickAction: utils.throttle(function(e) {
    var sts = e.currentTarget.dataset.sts;
    console.log('订单id = ',sts);
    this.setData({
      sts: sts
    });
    console.log(e)
    console.log((new Date()).getSeconds())
  },600),   //延迟600ms

参考:https://mp.weixin.qq.com/s/3FZJ0nQLhj9PCi0pfBjc9A

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容