当前位置:网站首页>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>
边栏推荐
- Volley7 – networkdispatcher obtains data from the network [easy to understand]
- 基于ABP实现DDD--领域逻辑和应用逻辑
- Grafana connection tdengine reports an error 535
- Digital signal processing synthesis matlab design of dual tone multi frequency dialing system
- Lambda&Stream
- 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
- Invitation letter | "people, finance, tax" digital empowerment, vigorously promote retail enterprises to achieve "doubling" of economies of scale
- 如何创建索引
- How to create an index
- The first meta universe auction of Chen Danqing's printmaking works will open tomorrow!
猜你喜欢

BisinessCardGen

7.19 - daily question - 408

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

Soft test --- fundamentals of programming language (Part 2)

Automated test series selenium three kinds of waiting for detailed explanation
![[untitled]](/img/70/5db8a8df63a3fd593acf7f69640698.png)
[untitled]

Kusionstack open source | exploration and practice of kusion model library and tool chain

基于ABP实现DDD--领域逻辑和应用逻辑

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

Wireshark introduction and packet capturing principle and process
随机推荐
第三章 内核开发
[mindspore] [training warning] warning when executing training code
Is qiniu Business School reliable? Is it safe to open Huatai account recommended by the lecturer
Redis管道技术/分区
7.18 - daily question - 408
Promtool Check
7.24 party notice
Latest information of 2022 cloud computing skills competition
Managing databases in a hybrid cloud: eight key considerations
ASP rs.open SQL, Conn, what does 3, 1 stand for in 3,1?
Soft test --- fundamentals of programming language (Part 2)
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
C recursively obtains all files under the folder and binds them to the treeview control
px rem em
Server intranet and Extranet
Codeworks round 650 (Div. 3) ABCD solution
Join MotoGP Monster Energy British Grand Prix!
Principle of data proxy
C language word translation (to help understand the basic meaning of words) is updated from time to time
Unity3d calls between different script functions or parameters