如何使用 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. Django QuerySet API---数据库接口

    基本的创建与查询 # -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import T ...

  2. 有状态 无状态 stateful stateless monolithic architecture microservice architecture 单体架构

    为什么游戏公司的server不愿意微服务化? - 知乎 https://www.zhihu.com/question/359630395 我大概说了,方便测试,方便维护,方便升级,服务之间松耦合,可多 ...

  3. QT之——QTableWidget拖拽单元格并替换内容(进阶)

    所需待重写函数: [virtual] bool QObject::eventFilter(QObject *watched, QEvent *event); /* * Filters events i ...

  4. SpringBoot-文件系统-Excel,PDF,XML,CSV

    SpringBoot-文件系统-Excel,PDF,XML,CSV 1.Excel文件管理 1.1 POI依赖 1.2 文件读取 1.3 文件创建 1.4 文件导出 1.5 文件导出接口 2.PDF文 ...

  5. Java——方法

    Java 方法 Systom.out.println():其中,println()是一个方法(Method),而System是系统类(Class),out是标准输出对象(Object).这句话的意思是 ...

  6. Grafana+Influxdb+Telegraf监控mysql

    Grafana+Influxdb+Telegraf监控mysql 一.安装 1.1安装Grafana+influxdb+telegraf 1.2启动服务,添加开机启动 1.3查看grafana界面 二 ...

  7. GeoMesa,整体架构,创建Schema并导入数据

    GeoMesa,整体架构,创建Schema并导入数据 一.GeoMesa-整体架构 二.GeoMesa-创建Schema并导入数据 2.1 GeoTools Data 模块 2.2 索引管理 2.3 ...

  8. CCF-最优配餐(BFS)

    最优配餐   问题描述 栋栋最近开了一家餐饮连锁店,提供外卖服务.随着连锁店越来越多,怎么合理的给客户送餐成为了一个急需解决的问题.栋栋的连锁店所在的区域可以看成是一个n×n的方格图(如下图所示),方 ...

  9. 腾讯云TcaplusDB获新加坡MTCS最高等级安全认证

    近日,经过国际权威认证机构DNV GL的全面评估审核,TcaplusDB获得了新加坡多层云安全(以下简称"MTCS")T3级最高等级认证,这标志着TcaplusDB全面满足了新加坡 ...

  10. SpringMVC学习笔记2

    一.日期赋值 目标:在springMVC中日期赋值兼容性更广泛 不能直接处理,必须使用转换器1.定义转换器,实现接口Converter<From,To> package com.zy.co ...