当前位置:网站首页>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
}
}边栏推荐
- [QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
- Review of double pointer problems
- Sliding window problem review
- 【云原生】3.1 Kubernetes平台安装KubeSpher
- jdbc使用call调用存储过程报错
- Please wait while Jenkins is getting ready to work
- C AES encrypts strings
- Easy to understand IIC protocol explanation
- Game push image / table /cv/nlp, multi-threaded start
- F12 solve the problem that web pages cannot be copied
猜你喜欢

Fluent implements a loadingbutton with loading animation

Modbus protocol communication exception

Figure database ongdb release v-1.0.3

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

js Array 列表 实战使用总结

02. Develop data storage of blog project

02. 开发博客项目之数据存储

JS quick start (II)

Cve-2019-11043 (PHP Remote Code Execution Vulnerability)

nacos-高可用seata之TC搭建(02)
随机推荐
Force buckle 1189 Maximum number of "balloons"
In 2022, we must enter the big factory as soon as possible
Solution of QT TCP packet sticking
Problems encountered in installing mysql8 on MAC
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
UCF(暑期团队赛二)
[untitled]
Codeless June event 2022 codeless Explorer conference will be held soon; AI enhanced codeless tool launched
Configuration file converted from Excel to Lua
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
SQLite add index
注释、接续、转义等符号
Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
Vulhub vulnerability recurrence 72_ uWSGI
Imperial cms7.5 imitation "D9 download station" software application download website source code
Rce code and Command Execution Vulnerability
01. 开发博客项目之项目介绍
Knowledge points of circular structure
After the project is released, index Html is cached
Huawei equipment is configured with OSPF and BFD linkage