javascript 函数debounce来自react-lazyload

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 函数debounce来自react-lazyload相关的知识,希望对你有一定的参考价值。

export default function debounce(func, wait, immediate) {
  let timeout;
  let args;
  let context;
  let timestamp;
  let result;

  const later = function later() {
    const last = +(new Date()) - timestamp;

    if (last < wait && last >= 0) {
      timeout = setTimeout(later, wait - last);
    } else {
      timeout = null;
      if (!immediate) {
        result = func.apply(context, args);
        if (!timeout) {
          context = null;
          args = null;
        }
      }
    }
  };

  return function debounced() {
    context = this;
    args = arguments;
    timestamp = +(new Date());

    const callNow = immediate && !timeout;
    if (!timeout) {
      timeout = setTimeout(later, wait);
    }

    if (callNow) {
      result = func.apply(context, args);
      context = null;
      args = null;
    }

    return result;
  };
}

以上是关于javascript 函数debounce来自react-lazyload的主要内容,如果未能解决你的问题,请参考以下文章

javascript函数的节流[throttle]与防抖[debounce]

JavaScript函数节流(throttle)与函数去抖(debounce)

JavaScript 防抖(debounce)和节流(throttle)

WEB开发者必备的7个JavaScript函数

js 函数节流 jQuery throttle/debounce

javascript的防抖和节流