当前位置:网站首页>Application scenarios of js anti-shake function and function throttling
Application scenarios of js anti-shake function and function throttling
2022-08-02 09:32:00 【Ape Code takes the lead】
Anti-shake function
Definition of the anti-shake function: when the event is triggered, the trigger is delayed, and it is triggered only once. If it is triggered again before the trigger, the delay will be refreshed again.
The beauty of the anti-shake function is that after the trigger, it will delay the trigger. If the next trigger is triggered within this delay time, the delay will be reset, which greatly improves the efficiency.
function debounce(callback,time = 300){let t = 0;return function(){clearTimeout(t);t = setTimeOut(callback,time);}}
The anti-shake function is applied to the scroll bar sliding event
Let's first look at when the anti-shake function is not defined - define a scroll bar scroll event - when the mouse slides, determine the distance of the scroll bar from the top, if it is less than a certain value, it will be hidden, if it is greater than a certain value, it will be displayed
window.onscroll = function(){let num = document.body.scrollTop || document.documentElement.scrollTop;console.log("Call once");if(parseInt(num) > 300){document.getElementById("imgs").style = "display:block";}else{document.getElementById("imgs").style = "display:none";}};
As you can see, the function will be called so many times with a slight mouse swipe.Calling n times, there will be unknown n*n judgments. Obviously, this efficiency is too low.
Look at the use of the anti-shake function again.
//Anti-shake functionfunction debounce(callback,time=300){let t;return function(){clearTimeout(t);t = setTimeout(callback,time);}}window.onscroll = debounce(returntop,300);//function returntop(){console.log("Call once");let num = document.body.scrollTop || document.documentElement.scrollTop;if(parseInt(num) > 300){document.getElementById("imgs").style = "display:block";}else{document.getElementById("imgs").style = "display:none";}}
window.onscroll = debounce(returntop,300);
Bind the function debounce(returntop,300) to the mouse scroll event
In the process of code execution, the debounce function will be called once.In this way, the function bound to the mouse scroll event is actually the function returned by the debounce function.
In this way, every time the mouse is slid once, after the time, it will be judged whether the top icon should be hidden or displayed. If it is slid again within the time, the last delay will be cleared, and then come againa new delay.In this way, the efficiency of the code will be greatly improved.
Anti-shake function can also be applied to search boxes (such as Baidu), automatic saving of text editors.The principle is the same as above.
Function throttling
Principle of function throttling: If the time is continuously triggered, it will only be triggered once every period of time, and the continuous trigger in the middle will not be executed.
function throlle(callback,time=300){let oldtime = new Date().getTime();return function(){let now = new Date().getTime();if(now - oldtime > time){callback();oldtime = now;}}}
Apply to the login button - if the function throttling is not set, when the user clicks to log in continuously, the background will accept n times, so the pressure on the background server will increase greatly.
If the login button click event binding function is throttled,
Example: document.getElementById("").οnclick=throlle(login,5000);
function login(){...}
Bind the throttling function to the login function. The click event binding is actually the function returned by the throlle function. When the code is executed, the thtolle function is called first to obtain a timestamp. If the click event occurs, another timestamp is obtained.If the difference between the two values is greater than the time, the login function will be called. If it is less than the time, the login function will not be executed, thus avoiding the pressure of the user to continuously click to log in to the server.
Function throttling can also be applied to scroll bar listener events.
Difference
Function throttling No matter how frequently the event is triggered, it will ensure that the real event handler will be executed once within the specified event, and the anti-shake function will only trigger the function once after the last event.
边栏推荐
- 中国发布丨滴滴因违反网络安全法等被罚80.26亿元!调查细节公布
- node制作一个视频帧长图生成器
- 了解下C# 不安全代码
- Docker内MySQL主从复制学习,以及遇到的一些问题
- SAP 云平台上一种 Low Code Development(低代码开发)解决方案
- day1-机器学习-回归问题
- 向量组的线性相关性
- AutoJs学习-密码生成器
- Re22:读论文 HetSANN An Attention-based Graph Neural Network for Heterogeneous Structural Learning
- CFdiv2-The Number of Imposters-(两种点集图上染色问题总结)
猜你喜欢
随机推荐
剑指offer专项突击版第17天
动态规划每日一练(3)
中国发布丨滴滴因违反网络安全法等被罚80.26亿元!调查细节公布
The god-level Alibaba "high concurrency" tutorial "basic + actual combat + source code + interview + architecture"
刷题错题录1-隐式转换与精度丢失
leetcode 62. Unique Paths(独特的路径)
AutoJs学习-存款计算器
Docker内MySQL主从复制学习,以及遇到的一些问题
RetinaFace: Single-stage Dense Face Localisation in the Wild
在全志V853开发板试编译QT测试
李航《统计学习方法》笔记之朴素贝叶斯法
深度学习汇报(4)
Navicat连接MySQL时弹出:1045:Access denied for user ‘root’@’localhost’
Implementation of mysql connection pool
单机部署flink,创建oracle19c rac的连接表时报错 ORA-12505 ,怎么回事?
AutoJs学习-实现科赫雪花
谈谈对Volatile的理解
Hikari连接池源码解读
【微信小程序】本地服务页面案例实现
主流监控系统工具选型及落地场景参考