如何使用 js 实现一个 throttle 函数

  1. 原理

  2. 实现方式


"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description 节流 throttle
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/ const log = console.log; // 节流: 是指在指定的单位时间内,如果重复触发相同的事件,则忽略后面的事件,直到上一次的事件执行完成,才会重新开始执行下一次的事件! function throttle(callback, timer = 1000) {
let id = null;
let flag = true;
return function() {
if(!id && flag) {
id = setTimeout(() => {
callback();
clearTimeout(id);
// flag = true;
}, timer);
} else {
log(`等待中,忽略后面事件的执行!`);
// flag = false;
}
}
} const cb = () => log(`callback function!`) const test = throttle(cb, 3000); log(`test`, test);
test();
setTimeout(() => {
log(`test2`);
test();
}, 1000);
setTimeout(() => {
log(`test3`);
test();
}, 2000); /* $ node throttle.js test [Function]
test2
等待中,忽略后面事件的执行!
test3
等待中,忽略后面事件的执行!
callback function! */

this & arguments



"use strict";

/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description 节流 throttle
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/ const log = console.log; // 节流: 是指在指定的单位时间内,如果重复触发相同的事件,则忽略后面的事件,直到上一次的事件执行完成,才会重新开始执行下一次的事件! function throttle(callback, timer = 1000) {
let id = null;
let flag = true;
return function() {
// const args = [...arguments];
const args = arguments;
const that = this;
// function (this, arguments)
if(!id) {
id = setTimeout(function () {
log(`that`, that)
log(`this`, this)
log(`args`, args)
callback.call(that, [...args]);
clearTimeout(id);
// flag = true;
}, timer);
} else {
log(`等待中,忽略后面事件的执行!`);
// flag = false;
}
// if(!id && flag) {
// id = setTimeout(() => {
// callback();
// clearTimeout(id);
// // flag = true;
// }, timer);
// } else {
// log(`等待中,忽略后面事件的执行!`);
// // flag = false;
// }
}
} const cb = (args) => log(`callback function!`, args); const test = throttle(cb, 3000); log(`test`, test);
// test();
test(`args = arguments`, 1);
setTimeout(() => {
log(`test2`);
test(`args = arguments`, 2);
}, 1000);
setTimeout(() => {
log(`test3`);
test(`args = arguments`, 2);
}, 2000); /* $ node throttle.js test [Function]
test2
等待中,忽略后面事件的执行!
test3
等待中,忽略后面事件的执行!
callback function! */ /* $ node throttle.js
test [Function]
test2
等待中,忽略后面事件的执行!
test3
等待中,忽略后面事件的执行!
that undefined
this Timeout {
_idleTimeout: 3000,
_idlePrev: null,
_idleNext: null,
_idleStart: 37,
_onTimeout: [Function],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(refed)]: true,
[Symbol(asyncId)]: 5,
[Symbol(triggerId)]: 1
}
args [Arguments] { '0': 'args = arguments', '1': 1 }
callback function! [ 'args = arguments', 1 ]
*/
  1. 总结

refs

https://www.cnblogs.com/xgqfrms/p/11886342.html

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


如何使用 js 实现一个 throttle 函数的更多相关文章

  1. 如何使用 js 实现一个 debounce 函数

    如何使用 js 实现一个 debounce 函数 原理 防抖: 是指在指定的单位时间内,如果重复触发了相同的事件,则取消上一次的事件,重新开始计时! 实现方式 "use strict&quo ...

  2. 如何用 js 实现一个 apply 函数

    如何用 js 实现一个 apply 函数 原理 实现方式 总结 refs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen ...

  3. 如何用 js 实现一个 call 函数

    如何用 js 实现一个 call 函数 原理 实现方式 总结 refs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc ...

  4. 如何用 js 实现一个 sleep 函数

    如何用 js 实现一个 sleep 函数 原理 实现方式 总结 refs js sleep xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  5. 如何用 js 实现一个 new 函数

    如何用 js 实现一个 new 函数 原理 new 关键字实现经过了如下过程 创建一个空对象 obj = {} 链接到原型 obj.proto = constructor.prototype 绑定 t ...

  6. 如何用 js 实现一个 bind 函数

    如何用 js 实现一个 bind 函数 原理 实现方式 总结 refs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc ...

  7. 使用原生JS封装一个动画函数

    最近一直在忙项目,很少有时间回顾之前的知识,今天刚好要做一个轮播,因为对兼容性有一定的要求,使用了各种插件和库中的轮播,效果都不是很理想,一怒之下,使用原生JS封装了一个轮播组件,其中重要的功能就是一 ...

  8. JS魔法堂:函数节流(throttle)与函数去抖(debounce)

    一.前言 以下场景往往由于事件频繁被触发,因而频繁执行DOM操作.资源加载等重行为,导致UI停顿甚至浏览器崩溃. 1. window对象的resize.scroll事件 2. 拖拽时的mousemov ...

  9. js中如何在一个函数里面执行另一个函数

    1.js中如何在函数a里面执行函数b function a(参数c){ b(); } function b(参数c){ } 方法2: <script type="text/javasc ...

随机推荐

  1. 翻页bug 在接口文档中应规范参数的取值区间 接口规范

    <?php$a=array("red","green","blue","yellow","brown&q ...

  2. 拓扑排序(topo sort)之 最大食物链计数( 洛谷P4017)

    前言: 复习复习拓扑排序,自己把自己弄没了/kk 题目传送门 简化题意: 在一个DAG中,求从所有入度为0的点到所有出度为0的点路径的条数 md理解错题意把自己卡了半天,生物学的好的就可以直接理解为求 ...

  3. 利用Javascript制作网页特效(图像特效)

    图像是文本的解释和说明,在网页中的适当位置放置一些图像,不仅可以使文本更加容易阅读,而且可以使网页更加具有吸引力. 当鼠标指针经过图像时图像振动效果 ①:在head标签内输入以下代码: <sty ...

  4. Grafana部署监控docker服务

    Grafana部署监控docker服务 一.使用InfluxDB+cAdvisor+Grafana配置Docker监控 1.1Docker监控组件 1.2cAdvisor: 1.3Docker监控安装 ...

  5. 手机用itunes安装更新系统

    1.[Shift+更新]:仅对固件进行更新,保留现有资料和已经安装的程序.但是已经越狱的iPhone或iPad禁止使用此方法!否则有后遗症!没有越狱的iPhone或iPad则可以直接使用此方式.2.[ ...

  6. Spring 启动脚本

    if [ $# != 3 ];then echo 'option-1: start,stop or restart.' echo 'option-2: 请传入jar路径' echo 'option-3 ...

  7. Educational Codeforces Round 21

    Educational Codeforces Round 21  A. Lucky Year 个位数直接输出\(1\) 否则,假设\(n\)十进制最高位的值为\(s\),答案就是\(s-(n\mod ...

  8. HDU5407 CRB and Candies 【LCM递推】

    HDU5407 CRB and Candies 题意: 计算\(LCM(C(n,0),C(n,1),C(n,2),\cdots,C(n,n-1),C(n,n))\) \(n\le 10^6\) 题解: ...

  9. Codeforces Round #628 (Div. 2) A. EhAb AnD gCd(LCM & GCD)

    题意: GCD(a,b) + LCM(a,b) = n,已知 n ,求 a,b. 思路: 设 gcd(a, b) = k, a = xk, b = yk , k + ab / k = n xy = n ...

  10. 【洛谷 p3366】模板-最小生成树(图论)

    题目:给出一个无向图,求出最小生成树,如果该图不连通,则输出orz. 解法:Kruskal求MST. 1 #include<cstdio> 2 #include<cstdlib> ...