当前位置:网站首页>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>
边栏推荐
- 分页的相关知识
- 494. Target sum · depth first search · knapsack problem
- Verification of Kirchhoff's law and Multisim Simulation (engineering documents attached)
- How to implement the server anti blackmail virus system is a problem we have to consider
- [icore4 dual core core _arm] routine 22: LwIP_ UDP experiment Ethernet data transmission
- Educational codeforces round 89 (rated for Div. 2) ABC problem solution
- Detailed usage of iperf
- Join MotoGP Monster Energy British Grand Prix!
- What is the root password of MySQL initial installation
- unresolved external symbol [email protected] resolvent
猜你喜欢

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

Netease game Flink SQL platform practice

如果实现与在线CAD图中的线段实时求交点
![[mindspore ascend] [running error] graph_ In mode, run the network to report an error](/img/81/9e96182be149aef221bccb63e1ce96.jpg)
[mindspore ascend] [running error] graph_ In mode, run the network to report an error

Vegetable greenhouses turned into smart factories! Baidu AI Cloud helps Shouguang, Shandong build a new benchmark for smart agriculture

7.19 - daily question - 408
![[Bert] QA, reading comprehension, information retrieval](/img/3b/f632271be813cd71a44b204f5060c4.png)
[Bert] QA, reading comprehension, information retrieval
![Why does [mindspore ascend] [custom operator] repeatedly assign values to one tensor affect another tensor?](/img/e3/135ac1e6eade70082c205d16ab8e34.jpg)
Why does [mindspore ascend] [custom operator] repeatedly assign values to one tensor affect another tensor?

Detailed explanation of alexnet of paddlepaddle paper series (with source code)
![[mindspore] [training warning] warning when executing training code](/img/5a/978889301bdd02d6474c6e5723ad97.jpg)
[mindspore] [training warning] warning when executing training code
随机推荐
Find the median of two numbers in the fourth question of C language deduction (three methods)
Tool use of rookie tutorial -- View subclass (implementation class) class diagram in idea
Quartus: install cyclone 10 LP device library for quartus version 17.1
Educational events
Codeworks round 649 (Div. 2) ABC problem solution
Codeworks round 651 (Div. 2) ABCD solution
Redis管道技术/分区
分页的相关知识
Cloud native observability tracking technology in the eyes of Baidu engineers
[mindspore] [mode] spontaneous_ The difference between mode and graph mode
C recursively obtains all files under the folder and binds them to the treeview control
This visual is not connected to the presentationsource.
Director of Shanghai Bureau of culture and Tourism: safety is the lifeline of culture and tourism, and we are seizing the new track of yuancosmos
What can testers do when there is an online bug?
Kusionstack open source | exploration and practice of kusion model library and tool chain
Quartus:17.1版本的Quartus安装Cyclone 10 LP器件库
If real-time intersection with line segments in online CAD drawings is realized
Redis 事务学习有感
Financial RPA robot enables enterprises to open a new era of intelligence
How to implement a state machine?