当前位置:网站首页>防抖和节流
防抖和节流
2022-06-10 20:18:00 【是落落呢】
防抖和节流都是用来解决前端的事件过于频繁这个情况,考虑到资源的利用率和设备的负担,需要控制一下请求的次数。防抖和节流的目的是一样的,但是方法不一样。
防抖
防抖是只响应最后一次的事件,也就述说,假设已经触发了某一个事件,开始计时,如果在一段时间内,再一次触发这个事件的话,重新开始计时,直到计时完成,响应事件。
function fangdou(fn,delay){
let timer = null;
return function(){
if(timer)
clearTimeout(timer);
timer = setTimeout(()=>{
fn.call(this);
},delay)
}
}
使用时直接使用这个函数,fn就是响应的事件,delay就是计时时间。
节流
节流是控制两次响应事件之间的时间间隔,保证再这段时间内,不会再响应这个事件,时间一过,可以再次响应这个事件。
function jieliu(fn,delay) {
let ready = true;
return function (){
if(ready){
fn.call(this);
ready = false;
setTimeout(()=>{
ready = true;
},delay)
}
}
}
使用时直接使用这个函数,fn就是响应的事件,delay就是两次相应之间的时间间隔。
边栏推荐
- A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
- ros虚拟时间
- Video monitoring system storage control, bandwidth calculation method
- Realize OSD reverse color on YUV image according to background color
- Redis缓存雪崩
- Codeforces Round #798 (Div. 2)
- 1、 Vulkan develops theoretical fundamentals
- 自注意力(self-attention)和多头注意力(multi-head attention)
- LeetCode 进阶之路 - 字符串中的第一个唯一字符
- torch. nn. Simple understanding of parameter / / to be continued. Let me understand this paragraph
猜你喜欢
随机推荐
Leetcode advanced road - 125 Validate palindrome string
LeetCode 进阶之路 - 删除排序数组中的重复项
pytorch深度学习——神经网络卷积层Conv2d
Read the source code of micropyton - add the C extension class module (3)
自注意力(self-attention)和多头注意力(multi-head attention)
Mba-day21 linear programming problem
Asynchronous, thread pool (completablefuture)
Serial Print() and serial The difference of write() function, and the problem of hexadecimal and string sending and receiving format in serial port communication and detailed explanation of the conver
Finally, someone explained the difference among cookies, sessions and tokens. Detailed explanation, interview questions.
数据库系统概论 ---- 第一章 -- 绪论(重要知识点)
设计多层PCB板需要注意哪些事项?
Unity analyzes the rendering of built-in terrain and does some interesting things
How to use Diablo immortal database
堆排序与加强堆代码 用于记忆
Error code 1129, state HY000, host 'xxx' is blocked because of many connection errors
Game compatibility test (general scheme)
Will your company choose to develop data center?
Read the source code of micropyton - add the C extension class module (2)
Connexion MySQL errorcode 1129, State hy000, Host 'xxx' is Blocked because of many Connection Errors
App test case









