当前位置:网站首页>Function anti chattering and function throttling
Function anti chattering and function throttling
2022-07-22 05:52:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Preface
There are some browser events that we don't want to trigger very often , Such as resizing the window (onresize)、 Monitor scroll bar scrolling (onscroll), If these listening events need to call the interface, it may be called hundreds of times in a second , There must be something wrong with this .
Function anti shake (debounce)
If someone enters the elevator ( Triggering event ), The elevator will be in 10 Seconds later ( Execute event listener ), At this time, if someone enters the elevator again ( stay 10 Trigger the event again within seconds ), We have to wait again 10 Seconds to go ( Retime ).
function debounce(fn,wait){
var timer = null;
return function(){
clearTimeout(timer)
timer = setTimeout(()=>{
fn()
},wait)
}
}
function log(){
console.log(1)
}
window.addEventListener('scroll', debounce(log, 1000));
Function throttling (throttle)
Make sure that if the first person in the elevator comes in ,10 Deliver it on time in seconds , This time starts from the first person on the elevator , Don't wait for , If no one , Does not run . Think carefully , The above anti shake method still has some shortcomings . If the page is long , We've been scrolling the page , that log Methods will never be executed . So we can upgrade the above anti shake method .
function throttle(fn,wait,time){
var previous = null; // Record the last running time
var timer = null;
return function(){
var now = +new Date();
if(!previous) previous = now;
// When the difference between the last execution time and the current time is greater than the set execution interval , Just take the initiative to do it once
if(now - previous > time){
clearTimeout(timer);
fn();
previous = now;// After executing the function , Record the current time now
}else{
clearTimeout(timer);
timer = setTimeout(function(){
fn();
},wait);
}
}
}
function log(){
console.log(1)
}
window.addEventListener('scroll', throttle(log, 1000, 2000));
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/107480.html Link to the original text :https://javaforall.cn
边栏推荐
- js 图片转换base64 base64转换为file对象
- The darling of "all-optical" era - 400g optical module
- 国家互联网信息办公室有关负责人就对滴滴全球股份有限公司依法作出网络安全审查相关行政处罚的决定答记者问
- Detr code
- 20 -- 验证回文串
- How much commission does CITIC Securities charge for opening an account?? Is it safe to open an account?
- An idea of solving agile iteration of desktop application with applet Technology
- The ultra-low price of domestic chips, while being replaced by domestic products, has led to a large number of exports, putting pressure on American chips
- C#使用Objects Comparer进行对象比较
- Those violations in the store will be punished by the official secondary punishment, the most common four
猜你喜欢
前两天面了个腾讯拿 38K 出来的,让我见识到了基础的天花板,今天share给大家~
被罗敏制霸的直播间,躲得开陆正耀吗?
814. Binary tree pruning: simple recursive problem
GAN的发展系列二(PGGAN、SinGAN)
Onvif协议及协议测试工具使用详解
OpenAI正式宣布DALL-E向100万个用户开放测试版
Tensorflow入门教程(三十七)——DC-VNet
The state Internet Information Office made a decision on the administrative punishment related to the network security review of didi Global Co., Ltd. in accordance with the law
Case analysis of building cross department communication system on low code platform
[featured] expression package bucket map applet (drainage, traffic master, rights and interests take away CPS, with PC background management)
随机推荐
JS&&TS学习总结
今天面了个腾讯拿 38K 出来的,让我见识到了基础的天花板
2022年数据库审计产品排行榜-必看!
Expression package applet, sideline daily entry 400+
国家互联网信息办公室有关负责人就对滴滴全球股份有限公司依法作出网络安全审查相关行政处罚的决定答记者问
计算机考研数据库题库
The state Internet Information Office made a decision on the administrative punishment related to the network security review of didi Global Co., Ltd. in accordance with the law
NOIPD2T2 – 宝藏 题解
【微信公众号h5】授权
使用yarn
国内期货期货中信建投可以开户吗,安全吗?
Niuke online question brushing - day 3
【综合笔试题】难度 3.5/5,多解法热门二叉树笔试题
C语言解题——Number Sequence
爱奇艺抖音和好,微博躺枪?
solo 博客的 Bubble 皮肤 文章详情的顶部图片怎么去掉
Judging whether a person is reliable depends on these three points
Document operation management
HTTP cache policy, strong cache, negotiation cache
干货!高并发下秒杀商品,你必须知道的9个细节