当前位置:网站首页>Anti shake and throttling of JS
Anti shake and throttling of JS
2022-07-02 06:51:00 【Rivers I】
Here's the catalog title
Anti shake and throttling concept
Anti shake and throttling are both ways to optimize performance : It is aimed at high-frequency triggered events : such as Scroll Events scroll, User input events input
Function anti shake
debounce: The user keeps triggering events , Event handlers are not executed , Until the user does not trigger this event within the specified time , Then execute the event handler once ( Anti shake means that the event handler will only execute once )
Function throttling throttle: The user keeps triggering events , The event handler function will execute once after the specified time interval , It will not be executed repeatedly within the specified time ;( Throttling means that the event handler executes once every specified interval )
The difference between anti shake and throttling : Anti shake is to change multiple execution to last execution , Throttling is to change from multiple execution to execution every other time
Implementation of anti shake and throttling : Timers and timestamps ( To prevent variable contamination , Realize functions in the form of closures )
Refer to the website : https://www.cnblogs.com/youma/p/10559331.html
Anti shake implementation ( Timer implementation )
function debounce(fn, delay) {
//fn Is the function of real event processing /delay Is time
// Declare a timer in advance , The assignment is null
let timer = null;
return function() {
// The first thought
// if (!timer) {
// timer = setTimeout(fn, delay)
// } else {
// clearTimeout(timer);
// timer = setTimeout(fn, delay)
// }
// Lite version Shake proof : The key point is to clear the node of the timer
// if (timer) {
// clearTimeout(timer);
// }
// timer = setTimeout(fn, delay)
// in consideration of this Point to and pass parameters
let that = this;
let args = arguments;
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function() {
fn.apply(that, args)
}, delay)
}
}
// Real time handlers
function print() {
console.log('hello world');
}
window.onscroll = debounce(print, 2000);
// When rolling , Call a function
</script>
Throttling is achieved ( Timestamp implementation )
<script>
// This throttling function uses timestamps to make the first scrolling event execute a callback function , After that, every 1000ms Do it once ,
// In less than 1000ms Scrolling during this time period is not performed
// Throttling function Time stamp
function throttle(fn, delay) {
// Time of starting point
let start = Date.now();
return function() {
// Get the current time , adopt current time - Starting time = Time difference ,
// Judge Sum of time difference delay The pass of
var now = Date.now();
if (now - start >= delay) {
print()
// After the event handler executes once , It needs to be timed again
start = Date.now();
}
}
}
// Real event handlers
function print() {
console.log(' throttle ');
}
window.onscroll = throttle(print, 1000);
</script>
If you want to pass on the parameters , change this The direction of
<script>
// Throttling function Time stamp
function throttle(fn, delay) {
// Time of starting point
let start = Date.now();
return function() {
let that = this; //this Point to window
let args = arguments;
// Get the current time , adopt current time - Starting time = Time difference ,
// Judge Sum of time difference delay The relationship between
let diff = Date.now() - start
if (diff > delay) {
fn.apply(this, arguments)
// After the event handler executes once , It needs to be timed again , So get the present time
start = Date.now();
}
}
}
// Real event handlers
function print() {
console.log(' throttle ');
}
window.onscroll = throttle(print, 1000);
</script>
边栏推荐
- CVE-2015-1635(MS15-034 )远程代码执行漏洞复现
- unittest.TextTestRunner不生成txt测试报告
- Stress test modification solution
- Deployment API_ automation_ Problems encountered during test
- [literature reading and thought notes 13] unprocessing images for learned raw denoising
- 如何调试微信内置浏览器应用(企业号、公众号、订阅号)
- FE - weex 开发 之 使用 weex-ui 组件与配置使用
- MySQL index
- Sentinel Alibaba open source traffic protection component
- In depth study of JVM bottom layer (IV): class file structure
猜你喜欢
Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决
Solve the problem of bindchange event jitter of swiper component of wechat applet
PgSQL learning notes
Fe - wechat applet - Bluetooth ble development research and use
Latex compilation error I found no \bibstyle &\bibdata &\citation command
Win10: add or delete boot items, and add user-defined boot files to boot items
Huawei mindspire open source internship machine test questions
pytest(2) mark功能
Latex在VSCODE中编译中文,使用中文路径问题解决
[literature reading and thought notes 13] unprocessing images for learned raw denoising
随机推荐
Kali latest update Guide
ModuleNotFoundError: No module named ‘jieba. analyse‘; ‘ jieba‘ is not a package
js中map和forEach的用法
Flask migrate cannot detect db String() equal length change
pytest(3)parametrize参数化
如何调试微信内置浏览器应用(企业号、公众号、订阅号)
Storage space modifier in CUDA
(the 100th blog) written at the end of the second year of doctor's degree -20200818
JS delete the last bit of the string
默认google浏览器打不开链接(点击超链接没有反应)
Sentinel Alibaba open source traffic protection component
Stack (linear structure)
Warp matrix functions in CUDA
Promise中有resolve和无resolve的代码执行顺序
Latex 报错 LaTeX Error: The font size command \normalsize is not defined问题解决
No process runs when querying GPU, but the video memory is occupied
Loops in tensorrt
qq邮箱接收不到jenkins构建后使用email extension 发送的邮件(timestamp 或 auth.......)
Sqli labs customs clearance summary-page3
SQL注入闭合判断