当前位置:网站首页>Small case of throttling function
Small case of throttling function
2022-06-21 14:28:00 【Advanced mathematics volume II half price】
Today, let's write a simple throttling function
500ms Multiple internal clicks are only recorded once
The effect is as shown in the picture :
The picture hasn't been sent yet
html and css part :
<body>
Every time 500 Millisecond can be recorded once
<button id="btn"> Am I </button>
<div id="circle">0</div>
</body>
<style>
#circle {
width: 50px;
height: 50px;
border-radius: 50%;
background-color: rgb(199, 1, 1);
line-height: 50px;
text-align: center;
color: white;
opacity: 1.0;
transition: opacity .25s;
}
#circle.fade {
opacity: 0.0;
transition: opacity .25s;
}
</style>Javascript part :
function throttle(fn, time = 500){
let timer;
return function(...args){
if(timer == null){
fn.apply(this, args);
timer = setTimeout(() => {
timer = null;
}, time)
}
}
}
btn.onclick = throttle(function(e){
circle.innerHTML = parseInt(circle.innerHTML) + 1;
circle.className = 'fade';
setTimeout(() => circle.className = '', 250);
}); The implementation of this throttling function is relatively simple , Using the idea of higher-order function , Wrap the original function , timer stay 500ms Then it will be cancelled , That is, setting timer = null, If less than 500ms,timer It's not equal to null Would not have come to this if Inside the function , Will not call function,timer = null When , Will be called once first function.
The next article is to write a small case of anti shake function .
边栏推荐
- Qt-2-signal and slot
- Subshell
- Compile time annotation automatically generates dataholder
- English accumulation__ annoyance
- Qt-6-file IO
- C2 hiding of traffic encryption
- 2021 the latest selenium truly bypasses webdriver detection
- ARP interaction process
- Reptile Foundation_ urllib
- Understand the use of protobuf serialization protocol
猜你喜欢

Two of my essays

Automatic operation and maintenance 4 - variables and encryption in ansible

Numpy: basic package for high performance scientific computing & data analysis

DP question brushing record

Declare war on uncivilized code II

Compile time annotation automatically generates dataholder

Getting started with qt-1-ui

Using the path module to prompt errors in ts+vite projects

Taobao secsha plug-in

Chapter 5 - application layer
随机推荐
Installation and application of MySQL 8.0 under Linux
Qt-3-basic components
Detailed explanation of hashtable source code in C #
Use Matplotlib to draw the first figure
Reverse generate the corresponding DTD constraint according to the XML file
Teach you how to design interface automation test cases: extract interface information and analyze it
Promotion guide for large enterprises: material preparation, PPT writing and on-site defense
[Goo Goo donkey takeout rebate system] customer service configuration tutorial of takeout CPS project - (attached with picture and text building tutorial)
module ‘selenium. webdriver‘ has no attribute ‘PhantomJS‘
Record the processing process of slow response of primary system
Nodejs setting domestic source
Explain the design idea and capacity expansion mechanism of ThreadLocal in detail
Never change
Native JS implements login function, and local cookies save login information -- [call Netease cloud API interface] - super detailed explanation
Lamp architecture 5 - MySQL Cluster and master-slave structure
Solution of difficult and miscellaneous problems in MVN packaging
Summary of common libraries in machine learning
How is the network connected
"My" C code specification
Alibaba cloud log service is available in Net project