var debounce = function (func, wait, lossless) {
    var lastTimeout, alreadyDo = false;
    if (typeof lossless == 'undefined')
        lossless = true;
    return function () {
        var context = this,
              args = arguments;
        if (!alreadyDo) {
            alreadyDo = true;
            setTimeout(function () { alreadyDo = false }, wait);
            clearTimeout(lastTimeout);
            func.apply(context, args);
        } else if (lossless) {
            clearTimeout(lastTimeout);
            lastTimeout = setTimeout(function () {
                func.apply(context, args);
            }, wait);
        }
    };
}

///防抖函数
///防止事件函数高频执行,间隔wait毫秒执行
///lossless是否保存最后一次未到间隔时间的执行

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
  • 2021-10-04
  • 2022-12-23
  • 2021-09-22
  • 2022-02-06
猜你喜欢
  • 2022-12-23
  • 2021-05-26
  • 2022-12-23
  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案