当前位置:网站首页>Anti shake and throttling are easy to understand
Anti shake and throttling are easy to understand
2022-07-06 05:31:00 【Yue Suxi】
Shake proof : adopt setTimeout The way , Within a certain time interval , Change multiple triggers into one trigger
User triggered events too frequently , Only for the last time
// 1. First get dom node
var ipt = document.getElementById("input")
// 2. Define a variable to clear the timer
var t = null
// 3. Triggering event
ipt.oninput = function () {
// 4. Judge whether there is t
if (t !== null) {
// 5. If there is any, clear the timer
clearTimeout(t)
}
// 6. without t Assign the timer to t
t = setTimeout(() => {
console.log(this.value);
}, 1000)
}That's it , Within one second interval , Multiple clicks will clear the timer , The timer will be executed after the interval exceeds one second
Encapsulate the :
// 1. First get dom node
var ipt = document.getElementById("input")
ipt.oninput = debounce(function () {
console.log(this.value);
}, 1000)
// Encapsulate an anti shake
function debounce(fn, time) {
var t = null
return function () {
if (t !== null) {
clearTimeout(t)
}
t = setTimeout(() => {
// Need to use call change this Point to , Otherwise this Point to window
fn.call(this)
}, time)
}
}throttle : Control execution times
Control the execution times of high-frequency events
// throttle
window.onscroll = throttle(function () {
console.log("1");
}, 500)
function throttle(fn, time) {
let flag = true;
return function () {
if (flag) {
setTimeout(() => {
fn()
// After each timer execution flag Change it to true
flag = true
}, time);
}
flag = false
}
}边栏推荐
- 04. Project blog log
- UCF (summer team competition II)
- 01. Project introduction of blog development project
- Oracle deletes duplicate data, leaving only one
- [detailed explanation of Huawei machine test] statistics of shooting competition results
- Game push: image / table /cv/nlp, multi-threaded start!
- [detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
- LeetCode_字符串反转_简单_557. 反转字符串中的单词 III
- Modbus protocol communication exception
- In 2022, we must enter the big factory as soon as possible
猜你喜欢

趋势前沿 | 达摩院语音 AI 最新技术大全

04. Project blog log
![[leetcode16] the sum of the nearest three numbers (double pointer)](/img/99/a167b0fe2962dd0b5fccd2d9280052.jpg)
[leetcode16] the sum of the nearest three numbers (double pointer)

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Implementing fuzzy query with dataframe

Compilation et connexion de shader dans games202 - webgl (comprendre la direction)

First acquaintance with CDN

nacos-高可用seata之TC搭建(02)

Vulhub vulnerability recurrence 68_ ThinkPHP

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
HAC cluster modifying administrator user password
Nacos TC setup of highly available Seata (02)
02. Develop data storage of blog project
Promotion hung up! The leader said it wasn't my poor skills
Fluent implements a loadingbutton with loading animation
Pointer classic written test questions
Unity gets the width and height of Sprite
Promise summary
Summary of deep learning tuning tricks
注释、接续、转义等符号
Selective parameters in MATLAB functions
02. 开发博客项目之数据存储
CUDA11.1在线安装
Ad20 is set with through-hole direct connection copper sheet, and the bonding pad is cross connected
UCF(暑期团队赛二)
How to download GB files from Google cloud hard disk
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Imperial cms7.5 imitation "D9 download station" software application download website source code
Review of double pointer problems
Oracle query table index, unique constraint, field