美文网首页
防抖、节流

防抖、节流

作者: Ag_fronted | 来源:发表于2020-12-06 11:05 被阅读0次

1、防抖

const debounce = function (f1, wait) {
  let executeTime = null;
  return function () {
    clearTimeout(executeTime);
    executeTime = setTimeout(f1, wait, ...arguments);
  };
};

2、节流(时间戳)

const throttle = function (func, delay) {
  let prev = Date.now();
  return function () {
    if (Date.now() - prev >= delay) {
      func.apply(this, arguments);
      prev = Date.now();
    }
  };
};

3、节流(定时器)

const throttle = function (f1, wait) {
  let executeTime = null;
  return function () {
    if (!executeTime) {
      const argus = arguments;
      executeTime = setTimeout(function () {
        f1(...argus);
        executeTime = null;
      }, wait);
    }
  };
};

相关文章

网友评论

      本文标题:防抖、节流

      本文链接:https://www.haomeiwen.com/subject/vzhowktx.html