JavaScript函数节流和函数去抖知识点学习
作者:laozhang 时间:2024-06-07 15:59:54
概念
节流 (throttle) 让一个函数不要执行的太频繁,减少执行过快的调用,叫节流
去抖 (debounce) 去抖就是对于一定时间段的连续的函数调用,只让其执行一次
throttle 应用场景
DOM 元素的拖拽功能实现(mousemove)
射击游戏的 mousedown/keydown 事件(单位时间只能发射一颗 * )
计算鼠标移动的距离(mousemove)
Canvas 模拟画板功能(mousemove
搜索联想(keyup
监听滚动事件判断是否到页面底部自动加载更多:给 scroll 加了 debounce 后,只有用户停止滚动后,才会判断是否到了页面底部;如果是 throttle 的话,只要页面滚动就会间隔一段时间判断一次
debounce 应用场景
每次 resize/scroll 触发统计事件
文本输入的验证(连续输入文字后发送 AJAX 请求进行验证,验证一次就好)
函数去抖的实现
我们以scroll事件为例,探究如何是实现滚动一次窗口打印一个hello world 字符串。 如果不对其节流或者去抖:
window.onscroll = function () {
console.log('hello world');
}
这样每滚动一次,实际上会打印多个 hello world。 函数去抖背后的思路是指,某些代码不可能在没有间断的情况下连续执行。创建一个定时器,在指定的时间间隔之后运行代码。当第二次调用该函数时,它会清除前一次的定时器并设置另一个。如果前一个定时器已经执行过了,这个操作就没有任何意义。然而,如果前一个定时器尚未执行,其实就是将其替换为一个新的定时器。目的是只有在执行函数的请求停止了一段时间之后才执行。
《高程三》给出了最简洁最经典的去抖代码,如下:
function debounce(method, context) {
clearTimeout(method.tId);
method.tId = setTimeout(function() {
method.call(context);
}, 1000);
}
function print() {
console.log('hello world');
}
window.onscroll = function() {
debounce(print);
};
再做一些改动
function debounce(delay, action) {
var tId;
return function () {
var context = this;
var arg = arguments;
if (tId) clearTimeout(tId);
tId = setTimeout(function () {
action.apply(context, arg);
}, delay);
}
}
window.onscroll = debounce(1000, print);
函数节流的实现
函数节流就是让连续执行的函数,变为固定时间段间断地执行。 大概有两种方式实现。
其一使用时间戳来判断是否已经到回调执行时间,记录上次执行的时间戳,然后每次触发事件时执行回调,回调中判断当前时间戳距离上次执行时间戳的时间间隔是否有*s,如果是,则执行,并更新上次执行的时间戳,如此循环。
var throttle = function(delay, action) {
var last = 0;
return function() {
var curr = new Date();
if (curr - last > delay) {
action.apply(this, arguments);
last = curr;
}
}
}
第二种方法是使用定时器,比如,当scroll事件刚触发时,打印一个hello world ,然后设置一个1000ms的定时器,此后每次触发scroll事件,触发回调,如果已经存在定时器,则回调不执行方法,知道定时器出发,handler被清除,然后重新设置定时器。
var throttle = function(delay, action) {
var timeout;
var later = function () {
timeout = setTimeout(function(){
clearTimeout(timeout);
// 解除引用
timeout = null;
}, delay);
};
later();
if (!timeout) {
action.apply(this, arguments);
later();
}
}
更新方法:
function throttlePro(delay, action) {
var tId;
return function () {
var context = this;
var arg = arguments;
if (tId) return;
tId = setTimeout(function () {
action.apply(context, arg);
clearTimeout(tId);
// setTimeout 返回一个整数,clearTimeout 之后,tId还是那个整数,setInterval同样如此
tId = null;
}, delay);
}
}
来源:https://webfem.com/post/debounce
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mysql socket文件作用详解
JavaScript之Getters和Setters 平台支持等详细介绍
js数组与字符串的相互转换方法
numpy中loadtxt 的用法详解
Anaconda中pkgs文件夹及如何清空PKGS
![](https://img.aspxhome.com/file/2023/0/132400_0s.png)
golang中使用匿名结构体的方法
ASP ajax分页教程一
ASP页面内VBScript和JScript的交互
在Python程序中操作文件之flush()方法的使用教程
Python 中的lambda函数介绍
PHP实现克鲁斯卡尔算法实例解析
实例讲解使用原生JavaScript处理AJAX请求的方法
请正确认识MySQL对服务器端光标的限制
requests.gPython 用requests.get获取网页内容为空 ’ ’问题
![](https://img.aspxhome.com/file/2023/9/97089_0s.png)
ORACLE8的分区管理
Oracle 9i产品文档
原生JS仿QQ阅读点击展开、收起效果
![](https://img.aspxhome.com/file/2023/7/135697_0s.jpg)
mac系统安装Python3初体验
理想高通滤波实现Python opencv示例
![](https://img.aspxhome.com/file/2023/1/81461_0s.png)
win10系统Anaconda和Pycharm的Tensorflow2.0之CPU和GPU版本安装教程
![](https://img.aspxhome.com/file/2023/3/134853_0s.png)