当前位置:网站首页>Some of my understanding about anti shake and throttling
Some of my understanding about anti shake and throttling
2022-07-25 00:55:00 【Classmate Pan Pan】
What kind of impact will anti shake and throttling cause ?
Continuously triggered events will cause excessive pressure on the server , Or too much memory , Anti shake and throttling can be used to alleviate .
What is anti shake ?
Anti shake refers to events triggered continuously , stay n Seconds later, execute the callback , If n The event is triggered again within seconds , Then it's recalculation .
Use scenarios :
For example, the form submission button
Anti shake code implementation :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
*{
padding:0;margin:0;}
.scroll-box{
width : 100%;
height : 500px;
background:blue;
overflow : auto;
}
.scroll-item{
height:1000px;
width:100%;
/* background-color: green; */
}
</style>
<body>
<div class="scroll-box">
<div class="scroll-item"></div>
</div>
</body>
<script>
let debounce = function (fn, wait) {
let timeout = null;
return function () {
if (timeout !== null) clearTimeout(timeout);// If triggered multiple times, the last record delay will be cleared
timeout = setTimeout(() => {
fn.apply(this, arguments);
// Or directly fn()
timeout = null;
}, wait);
};
}
// Processing function
function handle() {
console.log(arguments)
console.log(Math.random());
}
// The test case
document.getElementsByClassName('scroll-box')[0].addEventListener("scroll", debounce(handle, 3000));
</script>
</html>
What is throttling ?
Throttling refers to events that are triggered continuously , stay n It must be executed once every second .
Use scenarios :
When the user pulls down and refreshes the list , Throttling is better than anti shaking
Throttling code implementation :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
*{
padding:0;margin:0;}
.scroll-box{
width : 100%;
height : 500px;
background:blue;
overflow : auto;
}
.scroll-item{
height:1000px;
width:100%;
/* background-color: green; */
}
</style>
<body>
<div class="scroll-box">
<div class="scroll-item"></div>
</div>
</body>
<script>
let settime = (func,howtime)=>{
let timers = null
return ()=>{
if(!timers){
timers = setTimeout(() => {
// func.apply(this,arguments)
func()
timers = null
},howtime);
}
}
}
function handle(){
console.log(arguments);
console.log(Math.random());
}
document.getElementsByClassName("scroll-box")[0].addEventListener("scroll",settime(handle,2000))
</script>
</html>
边栏推荐
- 分页的相关知识
- C # "learning code snippet" - recursively obtain all files under the folder
- [untitled]
- BisinessCardGen
- Redis 事务学习有感
- Join MotoGP Monster Energy British Grand Prix!
- ASP rs.open SQL, Conn, what does 3, 1 stand for in 3,1?
- Research and Multisim Simulation of linear circuit characteristics (engineering documents attached)
- Where is the most formal account opening for futures trading? Capital security?
- The troubleshooting process of a segment error (disassembly address troubleshooting)
猜你喜欢

Financial RPA robot enables enterprises to open a new era of intelligence

Quartus:17.1版本的Quartus安装Cyclone 10 LP器件库

7.19 - daily question - 408

Quartus: install cyclone 10 LP device library for quartus version 17.1

Unity panel control

7.18 - daily question - 408
![[leetcode weekly replay] game 83 biweekly 20220723](/img/db/c264c94ca3307d4363d3cf7f5d770b.png)
[leetcode weekly replay] game 83 biweekly 20220723

How to implement a state machine?

Wireshark packet capturing and rapid packet location skills
![[mindspore] [xception model] script statement is suspected to be wrong](/img/86/3174f9edadf4b815a76678551cbfa5.jpg)
[mindspore] [xception model] script statement is suspected to be wrong
随机推荐
Quartus: install cyclone 10 LP device library for quartus version 17.1
What are the functions of rank function
Dynamic programming-01 knapsack rolling array optimization
Redis管道技术/分区
Usage of atomicinteger (counter)
Find the median of two numbers in the fourth question of C language deduction (three methods)
[Bert] QA, reading comprehension, information retrieval
Measurement and Multisim Simulation of volt ampere characteristics of circuit components (engineering documents attached)
C recursively obtains all files under the folder and binds them to the treeview control
R language uses ISNA function to check whether the list and dataframe contain missing values, marks abnormal values in data columns in dataframe as missing values Na, and uses na.omit function to dele
Financial RPA robot enables enterprises to open a new era of intelligence
Codeworks round 651 (Div. 2) ABCD solution
[mindspore] [mode] spontaneous_ The difference between mode and graph mode
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
Detailed usage of iperf
Ggplot2 visual faceting, visual faceted ridge plot with facet_wrap, and customize the background color of the faceted icon title box
asp rs.open sql,conn,3,1中3,1代表什么?
C language force buckle the eighth question of string conversion integer. Ergodic method
How to implement the server anti blackmail virus system is a problem we have to consider
The font changes with the change of the form