当前位置:网站首页>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
}
}边栏推荐
- Nacos TC setup of highly available Seata (02)
- Pix2pix: image to image conversion using conditional countermeasure networks
- 28io stream, byte output stream writes multiple bytes
- C Advanced - data storage (Part 1)
- Force buckle 1189 Maximum number of "balloons"
- 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
- First acquaintance with CDN
- Hyperledger Fabric2. Some basic concepts of X (1)
- 01. 开发博客项目之项目介绍
- Promotion hung up! The leader said it wasn't my poor skills
猜你喜欢

Problems encountered in installing mysql8 on MAC
![[leetcode] 18. Sum of four numbers](/img/06/c160b47d756290e5474e4c07e68648.png)
[leetcode] 18. Sum of four numbers

Simple understanding of interpreters and compilers

Remember an error in MySQL: the user specified as a definer ('mysql.infoschema '@' localhost ') does not exist

Ora-01779: the column corresponding to the non key value saving table cannot be modified

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

Using stopwatch to count code time

27io stream, byte output stream, OutputStream writes data to file

F12 solve the problem that web pages cannot be copied
随机推荐
After the project is released, index Html is cached
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
[QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
【华为机试真题详解】统计射击比赛成绩
UCF (2022 summer team competition I)
Easy to understand I2C protocol
Imperial cms7.5 imitation "D9 download station" software application download website source code
Yyds dry inventory SSH Remote Connection introduction
【LeetCode】18、四数之和
Drive development - the first helloddk
HAC集群修改管理员用户密码
Codeforces Round #804 (Div. 2) Editorial(A-B)
ByteDance program yuan teaches you how to brush algorithm questions: I'm not afraid of the interviewer tearing the code
毕业设计游戏商城
Codeless June event 2022 codeless Explorer conference will be held soon; AI enhanced codeless tool launched
Golang -- TCP implements concurrency (server and client)
Codeforces Round #804 (Div. 2) Editorial(A-B)
初识CDN
Remember an error in MySQL: the user specified as a definer ('mysql.infoschema '@' localhost ') does not exist
How to download GB files from Google cloud hard disk