当前位置:网站首页>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))
边栏推荐
猜你喜欢

Redis network IO

PVT的spatial reduction attention(SRA)

PyTorch自定义CUDA算子教程与运行时间分析

Hangzhou E-Commerce Research Institute released an explanation of the new term "digital existence"

一些实用、常用、效率越来越高的 Kubernetes 别名

CUDA programming-02: first knowledge of CUDA Programming

NiO Summary - read and understand the whole NiO process

C# 窗体应用常用基础控件讲解(适合萌新)

苹果降价600元,对本就溃败的国产旗舰手机几乎是毁灭性打击
![[acl2020] a novel method of component syntax tree serialization](/img/24/b8ec489966f7b1deef82b2eefa4d1b.png)
[acl2020] a novel method of component syntax tree serialization
随机推荐
【进程间通信IPC】- 信号量的学习
B tree
微信安装包从0.5M暴涨到260M,为什么我们的程序越来越大?
PVT的spatial reduction attention(SRA)
Tensorflow loss function
Qdoublevalidator does not take effect solution
5g failed to stimulate the development of the industry, which disappointed not only operators, but also mobile phone enterprises
Interface test tool - JMeter pressure test use
Mangodb simple to use
Deep understanding of Kalman filter (3): multidimensional Kalman filter
Principle of flex:1
ctfshow 终极考核
Encountered 7 file(s) that should have been pointers, but weren‘t
js call和apply
软件测试功能测试全套常见面试题【功能测试-零基础】必备4-1
Encountered 7 file(s) that should have been pointers, but weren‘t
TensorFlow损失函数
拍卖行做VC,第一次出手就投了个Web3
Five kinds of 2D attention finishing (non local, criss cross, Se, CBAM, dual attention)
QT uses SQLite to open multiple database files at the same time