当前位置:网站首页>防抖和节流
防抖和节流
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就是两次相应之间的时间间隔。
边栏推荐
- 在YUV图像上根据背景色实现OSD反色
- What is the difference between localhost and 127.0.0.1?
- Leetcode divides the array so that the maximum difference is k
- Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?
- Realize OSD reverse color on YUV image according to background color
- 游戏兼容性测试(通用方案)
- LeetCode:497. Random points in non overlapping rectangles -- medium
- 分布式服务理论基础
- ^30H5 Web Worker多线程
- H265 Nalu type judgment and SPS data analysis
猜你喜欢

What is the difference between localhost and 127.0.0.1?

View play and earn will lead crypto games astray

Calculus review 1

你的公司会选择开发数据中台吗?

详解MATLAB中与矩阵运算有关的算术运算符(加、减、乘、除、点乘、点除、乘方)

"O & M youxiaodeng" self service account unlocking tool

Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"

20192407 2021-2022-2 experimental report on Experiment 8 of network and system attack and Defense Technology

微积分复习1

連接mysql報錯 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors
随机推荐
微积分复习1
Power set V4 recursion of brute force method /1~n
Construction of RT thread smart win10 64 bit compilation environment
Realize OSD reverse color on YUV image according to background color
Read the source code of micropyton - add the C extension class module (3)
CET-6 - Business English - the last recitation before the test
leetcode 划分数组使最大差为 K
Identity and access management (IAM)
一、Vulkan开发理论基础知识
pytorch深度学习——神经网络卷积层Conv2d
从h264实时流中提取Nalu单元数据
2 pcs share a set of keyboard and mouse
torch. nn. Simple understanding of parameter / / to be continued. Let me understand this paragraph
"O & M youxiaodeng" self service account unlocking tool
Theoretical basis of distributed services
记录一下今天的MySQL故障
User defined date component. The left and right buttons control forward or backward year, month, week and day turning
MySQL service startup failed
Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"
LeetCode 进阶之路 - 搜索插入位置