Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

函数节流 #5

Open
bosens-China opened this issue Aug 12, 2019 · 0 comments
Open

函数节流 #5

bosens-China opened this issue Aug 12, 2019 · 0 comments
Labels
深入系列 主要介绍实现的思路

Comments

@bosens-China
Copy link
Owner

本来打算从零写一个,不过意外看到一篇好文,下面就根据他的思路来实现一个节流,JavaScript专题之跟着 underscore 学节流

前言

节流有两种实现,第一种使用定时器,另外一种则是根据时间戳,判断两次执行的时间间隔,下面简单些一下这两种的实现。

  • 定时器
function throttle(fn, time) {
  let timing = null;
  return function(...rest) {
    if(!timing) {
      timing = setTimeout(() => {
        timing = null;
        fn.apply(this, rest);
      }, time);
    }
  };
};
  • 时间戳
function throttle (fn, time) {
  let times = 0;
  return function (...rest) {
    const s = time - (new Date().getTime() - times)
    if (s <= 0) {
      times = new Date().getTime();
      fn.apply(this, rest);
    }
  };
};

下面就是将两者结合起来

节流

// 节流
// initial初始立即执行,front最后执行一次
function throttle (fn, time, { initial = true, front = false } = {}) {
  // 上一次执行时间
  let times = 0;
  // 定时器
  let timing = null;
  function next (...args) {
    // 判断条件,如果不是初始执行,改用定时器方法执行
    if (!initial && !times) {
      times = new Date().getTime();
    }
    const now = time - (new Date().getTime() - times);
    // now > time是判断更改系统时间的边界问题,now正常情况下,肯定小于time
    if (now <= 0 || now > time) {
      if (!timing) {
        clearTimeout(timing);
        timing = null;
      }
      times = new Date().getTime();
      fn.apply(this, args);
      // 清理一下内存
      if (!timing) {
        args = null;
      }
    } else if (!timing && front) {
      timing = setTimeout(() => {
        timing = null;
        times = initial ? 0 : new Date().getTime();
        fn.apply(this, args);
        if (!timing) {
          args = null;
        }
      }, time);
    }
  }
  // 取消
  next.cancel = function () {
    clearTimeout(timing);
    timing = null;
    // 为0即可,上面有初始判断条件
    times = 0;
  };
  return next;
};

注意

上面有两个参数,但是不能同时设置为false

@bosens-China bosens-China added the JavaScript JavaScript系列的文章 label Aug 12, 2019
@bosens-China bosens-China changed the title 实现一个节流函数 javascript基础系列之实现一个节流函数 Dec 18, 2019
@bosens-China bosens-China added 深入系列 主要介绍实现的思路 and removed JavaScript JavaScript系列的文章 labels Dec 18, 2019
@bosens-China bosens-China changed the title javascript基础系列之实现一个节流函数 javascript专题系列之实现一个节流函数 Dec 18, 2019
@bosens-China bosens-China changed the title javascript专题系列之实现一个节流函数 节流函数 Dec 21, 2021
@bosens-China bosens-China changed the title 节流函数 深入系列之函数节流 Dec 21, 2021
@bosens-China bosens-China changed the title 深入系列之函数节流 函数节流 Dec 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
深入系列 主要介绍实现的思路
Projects
None yet
Development

No branches or pull requests

1 participant