当前位置:网站首页>节流和防抖
节流和防抖
2022-06-23 22:18:00 【qq_53280805】
节流和防抖的目的:
都是为了限制函数的执行频次,以优化函数触发频率过高导致响应速度跟不上触发频率,防止在短时间内频繁触发同一事件而出现延迟,假死或者卡顿的现象。节流和防抖的区别:
节流:目前有一事件A设置了定时器,那么在delay之前触发,都只会触发一次。
防抖:如果在delay之前重新触发,那么定时器会不断重新计时,最终会在最后一次完后才执行,对于需要实时响应的,应该用节流。
3:例子
// 节流`
function throttle(fun,delay){
let flag = true;
return function(){
if(!flag){
return;
}
flag = false;
setTimeout(()=>{
fun.apply(this,arguments);
flag = true;
},delay)
}
}
function output(){
console.log('节流',Math.random());
}
// 防抖
function antiShake(fun,delay){
let timeout = null;
return function(e){
clearTimeout(timeout);
timeout = setTimeout(()=>{
fun.apply(this,argument);
},delay)
}
}
function play(){
console.log('防抖');
}
边栏推荐
- Digital supply chain management system for metallurgical industry: platform lean enterprise management to help the high-quality development of the industry
- log Network Execution Time
- 数字物业管理成趋势,传统物业公司如何通过转型实现数字化蝶变?
- iNFTnews | 创造者经济的未来在Web3世界中该去向何处?
- What are the good solutions for industrial control safety of production line
- Big guy on security privacy computing escort data security needs to pay attention to science and technology ethics at the same time
- Digital property management has become a trend. How can traditional property companies achieve digital butterfly change through transformation?
- MySQL导致索引失效的情况详解
- ACM. HJ89 24点运算 ●●●
- A cartoon reading app highly imitating Tencent comics
猜你喜欢

PyQt5_ Qtablewidget paging radio right-click menu control

Innovative lampblack supervision in the management measures for the prevention and control of lampblack pollution in Deyang catering service industry (Draft for comments)

6月25日PMP考试敏捷怎么考?替你分忧解难

Le roman du drapeau de l'imitation haute version flutter, apprenez - le

6 大完整开源项目,一次学个够

不容错过 | 华为内部资料--成功的项目管理PPT(123页)

Stm32-------adc (voltage detection)

PyQt5_QTableWidget分页单选右键菜单控件

Idea automatically generates unit tests, doubling efficiency!

高仿書旗小說 Flutter 版,學起來
随机推荐
Unity Text组件空格换行问题
一个人竟然撸了一个微博 APP
Goodbye, 2020, this bowl of poisonous chicken soup, I'll dry it first
High imitation Book flag novel flutter edition, learn it
Notepad++实用功能分享(正则行尾行首替换常用方法、文本比对功能等)
Improvement of DC power distribution with open hall current sensor
三款很酷很骚气的底部导航
6 大完整开源项目,一次学个够
Image segmentation - data annotation
再来一个高仿开眼的短视频APP
Idea automatically generates unit tests, doubling efficiency!
AutoCAD -- summarize three methods of drawing rounded corners in CAD
日化用品行业集团采购管理系统改变传统采购模式,降低采购成本
To ensure the safety of groups with special difficulties, Guangzhou Civil Affairs made every effort to do a good job in the three prevention work
【 GBASE的那些事儿】系列直播活动第02期《GBase 8s高可用技术及案例分析法》
Thinking (86): master-slave realization idea
Smart doc + Torna compatible version
Docker Deployment redis
What is the production process of enterprise website? How long does it take to design and build a website?
泰勒公式及常用展开