如何使用 js 实现一个 debounce 函数
如何使用 js 实现一个 debounce 函数
- 原理
防抖: 是指在指定的单位时间内,如果重复触发了相同的事件,则取消上一次的事件,重新开始计时!
- 实现方式
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description 防抖 & debounce
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
// 防抖: 是指在指定的单位时间内,如果重复触发了相同的事件,则取消上一次的事件,重新开始计时!
function debounce(callback, timer = 1000) {
let id = null;
return function() {
clearTimeout(id);
id = setTimeout(() => {
callback();
}, timer);
}
}
const cb = () => log(`callback function!`)
const test = debounce(cb, 3000);
log(`test`, test);
test();
setTimeout(() => {
log(`test2`);
test();
}, 1000);
setTimeout(() => {
log(`test3`);
test();
}, 2000);
/*
$ node debounce.js
test [Function]
test2
test3
callback function!
*/
this & arguments
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description 防抖 & debounce
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
// 防抖: 是指在指定的单位时间内,如果重复触发了相同的事件,则取消上一次的事件,重新开始计时!
function debounce(callback, timer = 1000) {
let id = null;
return function() {
// const args = [...arguments];
const args = arguments;
const that = this;
// function (this, arguments)
clearTimeout(id);
id = setTimeout(function () {
log(`that`, that)
log(`this`, this)
log(`args`, args)
callback.call(that, [...args]);
}, timer);
// Arrow Function (this)
// id = setTimeout(() => {
// callback();
// }, timer);
}
}
// const cb = () => log(`callback function!`);
const cb = (args) => log(`callback function!`, args);
const test = debounce(cb, 3000);
log(`test`, test);
test(`args = arguments`, 1);
setTimeout(() => {
log(`test2`);
test(`args = arguments`, 2);
}, 1000);
setTimeout(() => {
log(`test3`);
test(`args = arguments`, 3);
}, 2000);
/*
$ node debounce.js
test [Function]
test2
test3
callback function!
*/
/*
$ node debounce.js
test [Function]
test2
test3
that undefined
this Timeout {
_idleTimeout: 3000,
_idlePrev: null,
_idleNext: null,
_idleStart: 2044,
_onTimeout: [Function],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(refed)]: true,
[Symbol(asyncId)]: 11,
[Symbol(triggerId)]: 7
}
args [Arguments] { '0': 'args = arguments', '1': 3 }
callback function! [ 'args = arguments', 3 ]
*/
- 总结
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 实现一个 debounce 函数的更多相关文章
- 如何使用 js 实现一个 throttle 函数
如何使用 js 实现一个 throttle 函数 原理 实现方式 "use strict"; /** * * @author xgqfrms * @license MIT * @c ...
- 如何用 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 ...
- underscore.js中的节流函数debounce及trottle
函数节流 throttle and debounce的相关总结及想法 一开始函数节流的使用场景是:放止一个按钮多次点击多次触发一个功能函数,所以做了一个clearTimeout setTimeou ...
随机推荐
- Before you launch a goroutine, know when it will stop The Zen of Go
The Zen of Go https://the-zen-of-go.netlify.app/ Ten engineering values for writing simple, readable ...
- 算法总结篇---AC自动机
目录 写在前面 算法流程 引例: 概述: Trie树的构建(第一步) 失配指针(第二步) 构建失配指针 字典树和字典图 多模式匹配 例题 写在前面 鸣谢: OiWiki 「笔记」AC 自动机---Lu ...
- owncloud搭建
使用OwnCloud建立属于自己私有的云存储网盘 OwnCloud概述: OwnCloud 一款文件主机服务软件,就是我们平时使用的云存储,不过这是在自己主机的服务器上建立属于自己的私有云,OwnCl ...
- pythonchallenge总述
Pythonchallenge是一个过关式的解谜站点,使用的是经典在线解谜站点Not Pr0n的模式:根据提示找出下一关的网页地址.和Not Pr0n不同的是,在每一关里你都需要编写程序来寻找答案.虽 ...
- 织梦dedecms首页、列表页、文章页文章点击浏览次数实时调用方法
首先呢,先在根目录 /plus 目录下找到count.php 复制一份然后命名为viewclick.php(你也可以命名为你容易理解的名字)用编辑器将viewclick.php打开然后删除以下几行代 ...
- Scrambled Polygon POJ - 2007 极角排序
题意: 给你n个点,这n个点可以构成一个多边形(但是不是按顺序给你的).原点(0,0)为起点,让你按顺序逆序输出所有点 题解: 就是凸包问题的极角排序 用double一直Wa,改了int就可以了 // ...
- 灯光照射,圆形探测类问题(解题报告)<分层差分><cmath取整>
题目描述 一个n*n的网格图上有m个探测器,每个探测器有个探测半径r,问这n*n个点中有多少个点能被探测到. 输入输出格式 输入格式: (1<=r<n<=5000) (1<=m ...
- hdu5564 Clarke and digits
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission ...
- hdu-6699 Block Breaker
题意: 就是给你一个n行m列的矩形,后面将会有q次操作,每次操作会输入x,y表示要击碎第x行第y列的石块,当击碎它之后还去判断一下周围石块是否牢固 如果一个石块的左右两边至少一个已经被击碎且上下也至少 ...
- Codeforces Round #582 (Div. 3) A. Chips Moving
传送门 题解: 给你n个数的坐标,你需要把他们移动到一个位置,有两种移动方式 1.向左或者右移动2 2.向左或者右移动1,但是耗费1 求最小耗费 题解: 很简单就可以想到,看一下偶数坐标多还是奇数坐标 ...