当前位置:网站首页>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))
边栏推荐
- 【ACL2020】一种新颖的成分句法树序列化方法
- openharmony萌新贡献指南
- CUDA programming-02: first knowledge of CUDA Programming
- 网易笔试之解救小易——曼哈顿距离的典型应用
- 【每日算法Day 96】腾讯面试题:合并两个有序数组
- Activation functions commonly used in deep learning
- Specific methods and steps for Rockwell AB PLC to establish communication with PLC through rslinx classic
- NIO this.selector.select()
- Principle of flex:1
- PVT's spatial reduction attention (SRA)
猜你喜欢

Solve the problem of Chinese garbled code on the jupyter console

【进程间通信IPC】- 信号量的学习

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

软件测试功能测试全套常见面试题【功能测试-零基础】必备4-1

Low cost, low threshold, easy deployment, a new choice for the digital transformation of 48 million + small and medium-sized enterprises

Unity3d 2021 software installation package download and installation tutorial

npm install报错 强制安装

js call和apply
![[interprocess communication IPC] - semaphore learning](/img/47/b76c329e748726097219abce28fce8.png)
[interprocess communication IPC] - semaphore learning

Redis network IO
随机推荐
Replace restricts the text box to regular expressions of numbers, numbers, letters, etc
存储和计算引擎
Test picture
NiO Summary - read and understand the whole NiO process
Primary function t1744963 character writing
Is the operation of assigning values to int variables atomic?
The execution sequence of async/await, macro tasks and micro tasks
DNS域名空间
拍卖行做VC,第一次出手就投了个Web3
qt中使用sqlite同时打开多个数据库文件
Low cost, low threshold, easy deployment, a new choice for the digital transformation of 48 million + small and medium-sized enterprises
MySQL transaction
Deep understanding of Kalman filter (3): multidimensional Kalman filter
Aruba learning notes 10 security authentication portal authentication (web page configuration)
[daily algorithm day 96] Tencent interview question: merge two ordered arrays
ArkUI中的显式动画
软件测试功能测试全套常见面试题【功能测试-零基础】必备4-1
Solve the problem of Chinese garbled code on the jupyter console
Tensorflow模型训练和评估的内置方法
5G没能拉动行业发展,不仅运营商失望了,手机企业也失望了