如何使用 js 实现一个 throttle 函数
如何使用 js 实现一个 throttle 函数
原理
实现方式
"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 ]
*/
- 总结
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 函数的更多相关文章
- 如何使用 js 实现一个 debounce 函数
如何使用 js 实现一个 debounce 函数 原理 防抖: 是指在指定的单位时间内,如果重复触发了相同的事件,则取消上一次的事件,重新开始计时! 实现方式 "use strict&quo ...
- 如何用 js 实现一个 apply 函数
如何用 js 实现一个 apply 函数 原理 实现方式 总结 refs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen ...
- 如何用 js 实现一个 call 函数
如何用 js 实现一个 call 函数 原理 实现方式 总结 refs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc ...
- 如何用 js 实现一个 sleep 函数
如何用 js 实现一个 sleep 函数 原理 实现方式 总结 refs js sleep xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
- 如何用 js 实现一个 new 函数
如何用 js 实现一个 new 函数 原理 new 关键字实现经过了如下过程 创建一个空对象 obj = {} 链接到原型 obj.proto = constructor.prototype 绑定 t ...
- 如何用 js 实现一个 bind 函数
如何用 js 实现一个 bind 函数 原理 实现方式 总结 refs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc ...
- 使用原生JS封装一个动画函数
最近一直在忙项目,很少有时间回顾之前的知识,今天刚好要做一个轮播,因为对兼容性有一定的要求,使用了各种插件和库中的轮播,效果都不是很理想,一怒之下,使用原生JS封装了一个轮播组件,其中重要的功能就是一 ...
- JS魔法堂:函数节流(throttle)与函数去抖(debounce)
一.前言 以下场景往往由于事件频繁被触发,因而频繁执行DOM操作.资源加载等重行为,导致UI停顿甚至浏览器崩溃. 1. window对象的resize.scroll事件 2. 拖拽时的mousemov ...
- js中如何在一个函数里面执行另一个函数
1.js中如何在函数a里面执行函数b function a(参数c){ b(); } function b(参数c){ } 方法2: <script type="text/javasc ...
随机推荐
- 前端面试之CSS常用的选择器!
前端面试之CSS常用的选择器! 标签选择器 <style> /* <!-- 标签选择器 :写上标签名 -->*/ p { color: green; } div { color ...
- windows ping bat脚本
参考百度链接:https://zhidao.baidu.com/question/577024998.html 要求:1.从同级目录下读取iplist.txt文件内的ip/域名列表(每行一个):2.对 ...
- 干货 | 携程多语言平台-Shark系统的高可用演进之路
https://mp.weixin.qq.com/s/cycZslUlfyVNm2GVrZm1Cw 干货 | 携程多语言平台-Shark系统的高可用演进之路 原创 Fenlon 携程技术 2020-1 ...
- owners
community/owners.md at master · kubernetes/community https://github.com/kubernetes/community/blob/ma ...
- Routine Subroutine Coroutine 子程序 协程 子例程
https://en.wikipedia.org/wiki/Subroutine In computer programming, a subroutine is a sequence of prog ...
- 单机模拟配置Eureka集群
首先先提醒单机部署的重要点 如果使用一个ip地址(适用于单网卡)每个eureka实例使用不同的域名映射到同一个IP 如果每个eureka实例使用不同的IP(多网卡),要确保这些IP要都表示本地 本文假 ...
- ThinkPHP 漏洞利用
ThinkPHP thinkphp_5x_命令执行漏洞 受影响版本包括5.0和5.1版本 docker漏洞环境源码: https://github.com/vulnspy/thinkphp-5.1.2 ...
- Java 复习整理day06
Java api 章节除了一下列的常用类别的用时候查文档 1 package com.it.demo01_api; 2 3 import java.util.Scanner; 4 5 /* 6 案例: ...
- Yacc使用优先级
Yacc使用优先级 本示例是龙书4.9.2的示例,见图4-59. 和前一章一样,新建xUnit项目,用F#语言.起个名C4F59安装NuGet包: Install-Package FSharpComp ...
- sentinel流控规则校验之源码分析
前言: 上节给大家把sentinel流控整个执行大致过了,但涉及到最核心的流控算法还没有讲,先提前说明一下 sentinel用的流控算法是令牌桶算法,参考了Guava的RateLimiter,有读过R ...