当前位置:网站首页>[JS knowledge] easily understand JS anti shake and throttling
[JS knowledge] easily understand JS anti shake and throttling
2022-06-12 05:30:00 【Hehe, write】
js Anti shake and throttling are the knowledge points often asked in front-end interviews , Combine today moba Class games to explain .
1. Shake proof :
Continuous trigger does not execute , Execute after a period of time without triggering . It means after triggering the event n Function can only be executed once per second , If in n Seconds to trigger the event , The function execution time is recalculated .
Just like the return to the city among the kings , Each time I go back to the city, I need to read a note for a period of time , After reading the note, execute the operation of returning to the city . If the return to the city is triggered again when reading the note , Then the reading will be interrupted and the reading will be restarted . This is it. Shake proof .
The practical application :
// as follows , Continue to trigger scroll When an event is , Not implemented handle function , When 1000 No trigger in milliseconds scroll When an event is , Will delay the trigger scroll event
function debounce(fn, wait) {
var timeout = null;
return function() {
if(timeout !== null) clearTimeout(timeout);
timeout = setTimeout(fn, wait);
}
} // Processing function
function handle() {
console.log(Math.random());
}
// Scroll Events window.addEventListener('scroll', debounce(handle, 1000));
2. throttle :
Continuous triggering also performs , It's just that the execution frequency becomes lower . It means to trigger events continuously but in n Function is executed only once in seconds . Throttling dilutes the frequency of function execution .
Like ping in the king A equally , You are flat A How much is only related to your attack speed , It has nothing to do with your hand speed , Even if your hand speed is faster than the attack speed , Your Ping A The amount of is only related to the attack speed . This is it. throttle .
The practical application :
// as follows , Continue to trigger scroll When an event is , Not immediately handle function , every other 1000 Only once in milliseconds handle function
var throttle =function(func, delay) {
var prev = Date.now();
return function() {
var context = this;
var args = arguments;
var now = Date.now();
if (now - prev >= delay) {
func.apply(context, args);
prev = Date.now();
}
}
}
function handle() {
console.log(Math.random());}
window.addEventListener('scroll', throttle(handle, 1000));
3. Common ground
Is to limit the number of function executions , To improve the user experience .
边栏推荐
- Detailed explanation of WiFi 802.1x authentication process
- Pupanvr- an open source embedded NVR system (1)
- Test work summary - performance test indicators
- Necessary for Test Engineer -- package capturing tool fiddler
- tkinter使用WebView2网页组件(续篇)
- 4.3 simulate browser operation and page waiting (display waiting and implicit waiting, handle)
- Yolov5 realizes road crack detection
- Some optimization methods for UI Application of Qt5 on Hisilicon security platform
- Lvgl8.1 hi3536c platform use
- Pupanvr- establishment of development environment and diary, addition of some basic tool functions (3)
猜你喜欢

Stm32f4 ll library multi-channel ADC

Project requirements specification

Introduction to audio alsa architecture

Introduction to redis cluster

国企为什么要上市

Is the individual industrial and commercial door a legal person enterprise

beginning一款非常优秀的emlog主题v3.1,支持Emlog Pro

Wireshark filter rule

Nature | 给全球的新冠伤亡算一笔账

利用jieba库进行词频统计
随机推荐
Introduction to MMS memory optimization of Hisilicon MPP service
Reason: Canonical names should be kebab-case (‘-‘ separated), lowercase alpha-numeric characters and
Alsa architecture application aplay and amixer call relationship (from application layer to kernel driver)
49. ugly number
Project requirements specification
beginning一款非常优秀的emlog主题v3.1,支持Emlog Pro
59 - I. maximum value of sliding window
Servlet core technology
4.3 模拟浏览器操作和页面等待(显示等待和隐式等待、句柄)
ESP8266 Arduino OLED
Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class
Set common methods
Redis cluster cluster capacity expansion and data migration
Go 面向接口编程实战
What is thinking
How to deploy dolphin scheduler 1.3.1 on cdh5
Automated test - dark horse headline test project
Platform of ASoC framework driven by alsa
31. stack push in and pop-up sequence
16. Somme des trois plus proches