当前位置:网站首页>Function anti chattering throttling
Function anti chattering throttling
2022-07-27 09:09:00 【The gentle wind is attributed to you with a smile】
Function anti chattering and throttling are both methods to control the trigger frequency of events , There are also many application scenarios , Such as :
1. The input box keeps typing , Send the input content to the server for verification ;
2. Trigger click events many times ;
3.onSroll Scroll bar scroll Events ;
4.onmousemove Mouse movement events ;
Case study
To illustrate , Suppose a scenario : Mouse over a div, Trigger onmousemove event , The text inside it will display the coordinates of the current mouse . Its effect is like this
>> Function anti shake (debounce)
there “ Shaking, ” Is that the function is triggered , The general jitter is multiple and continuous . Suppose a function continues to execute multiple times , We want it to cool down and execute in a few seconds . That is, when the event continues to trigger, do not execute , Wait for a period of time after the last trigger .
>> Function throttling (throttle)
Throttling is to let the function execute in moderation , Instead of executing every trigger . That is to say, within the prescribed time , Only once .
>> Code implementation
/**
* @desc Function anti shake
* @param func function
* @param wait Delay execution in milliseconds
* @param immediate true Watch immediately ,false Not immediately
* The immediate version means that the function is executed immediately after the event is triggered , then n Does not trigger the event in seconds to continue the function's effect ( After the event is triggered for the first time , After the next trigger, if the time from the last trigger is less than n Seconds, then it won't be executed again , If it is greater than, it will execute )
* The non immediate version means that the function does not execute immediately after the event is triggered , But in n Seconds later , If in n Seconds to trigger the event , The function execution time is recalculated ( The first trigger does not execute , The pause time after the event is triggered exceeds n Seconds will be executed automatically )
*/
function debounce(func, wait, immediate) {
let timeout;
return function () {
let context = this;
let args = arguments;
if (timeout) clearTimeout(timeout);
if (immediate) {
var callNow = !timeout;
timeout = setTimeout(() => {
timeout = null;
}, wait)
if (callNow) func.apply(context, args)
}
else {
timeout = setTimeout(function () {
func.apply(context, args)
}, wait);
}
}
}
/**
* @desc Function throttling
* @param func function
* @param wait Delay execution in milliseconds
* @param type 1 Time stamp version of table ,2 Watch timer version
*/
function throttle(func, wait, type) {
let previous,timeout;
if (type === 1) {
previous = 0;
} else if (type === 2) {
// let \\timeout;
}
return function () {
let context = this;
let args = arguments;
if (type === 1) {
let now = Date.now();
if (now - previous > wait) {
func.apply(context, args);
previous = now;
}
} else if (type === 2) {
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args)
}, wait)
}
}
}
}
// call
document.getElementById("box-debounce").addEventListener('mousemove',debounce(function(e){
document.getElementById("box-debounce").innerHTML = e.clientX +","+e.clientY;
},1000,true))
// call
document.getElementById("box-throttle").addEventListener('mousemove',throttle(function(e){
document.getElementById("box-throttle").innerHTML = e.clientX +","+e.clientY;
},1000,2))
边栏推荐
- Some practical, commonly used and increasingly efficient kubernetes aliases
- npm install报错 强制安装
- Kibana uses JSON document data
- The execution sequence of async/await, macro tasks and micro tasks
- NiO Summary - read and understand the whole NiO process
- Test picture
- pollFirst(),pollLast(),peekFirst(),peekLast()
- 如何在B站上快乐的学习?
- Restful
- "Weilai Cup" 2022 Niuke summer multi school training camp 1
猜你喜欢
![[flutter -- geTx] preparation](/img/5f/96075fa73892db069db51fe789715a.png)
[flutter -- geTx] preparation

Mmrotate trains its dataset from scratch

Antdesign a-modal自定义指令实现拖拽放大缩小

Pyqt5 rapid development and practice 4.1 qmainwindow

Five kinds of 2D attention finishing (non local, criss cross, Se, CBAM, dual attention)

NiO Summary - read and understand the whole NiO process

苹果降价600元,对本就溃败的国产旗舰手机几乎是毁灭性打击

CUDA programming-02: first knowledge of CUDA Programming

B tree

Redis network IO
随机推荐
Understand various IOU loss functions in target detection
Software testing function testing a full set of common interview questions [function testing - zero foundation] essential 4-1
C# 窗体应用常用基础控件讲解(适合萌新)
拍卖行做VC,第一次出手就投了个Web3
qt中使用sqlite同时打开多个数据库文件
Easy language programming: allow the screen reading software to obtain the text of the label control
PVT的spatial reduction attention(SRA)
2036: [Blue Bridge Cup 2022 preliminary] statistical submatrix (two-dimensional prefix sum, one-dimensional prefix sum)
Matlab求解微分代数方程 (DAE)
500报错
Explain cache consistency and memory barrier
Encountered 7 file(s) that should have been pointers, but weren‘t
MySQL basic knowledge learning (I)
Ztree custom title attribute
Built in method of tensorflow model training and evaluation
MATLAB data import -- importdata and load functions
Intel, squeezed by Samsung and TSMC, finally put down its body to customize chip technology for Chinese chips
Explanation of common basic controls for C # form application (suitable for Mengxin)
Qt中发送信号时参数为结构体或者自定义的类怎么办?
How to optimize the deep learning model to improve the reasoning speed