当前位置:网站首页>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.
边栏推荐
- day1-机器学习-回归问题
- Re23:读论文 How Does NLP Benefit Legal System: A Summary of Legal Artificial Intelligence
- Pycharm (1) the basic use of tutorial
- The 17th day of the special assault version of the sword offer
- 大厂外包,值得拥有吗?
- PyCharm usage tutorial (detailed version - graphic and text combination)
- Gorilla Mux 和 GORM 的使用方法
- shell脚本
- Nodejs3day(express简介,express创建基本Web服务器,托管静态资源,nodemon下载及出现的问题,中间件,编写GET,POST,JSONP接口)
- Jenkins--部署--3.1--代码提交自动触发jenkins--方式1
猜你喜欢
随机推荐
cococreator 动态设置精灵
Redis数据结构
了解下C# 不安全代码
动态规划每日一练(2)
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
天地图给多边形加标注
1对1视频源码——快速实现短视频功能提升竞争力
In the whole development of chi V853 board tried to compile QT test
小程序云开发(十):渐变与动画
在 QT Creator 上配置 opencv 环境的一些认识和注意点
一文带你了解推荐系统常用模型及框架
初学者怎么快速学会SQL
Spend 2 hours a day to make up for Tencent T8, play 688 pages of SSM framework and Redis, and successfully land on Meituan
线程池的使用及ThreadPoolExecutor源码分析
软件exe图标变记事本或浏览器、360压缩打不开的几种应急解决方法
openpyxl 单元格合并
恋爱十不要
【微信小程序】本地服务页面案例实现
cococreator dynamically set sprite
干货|如何在海量文件系统中选择合适自己的文件系统









