当前位置:网站首页>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-高可用seata之TC搭建(02)
- Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
- Sword finger offer II 039 Maximum rectangular area of histogram
- Notes, continuation, escape and other symbols
- 02. 开发博客项目之数据存储
- HAC集群修改管理员用户密码
- 03. Login of development blog project
- MySQL time processing
- Qt TCP 分包粘包的解决方法
- Mysql高级篇学习总结9:创建索引、删除索引、降序索引、隐藏索引
猜你喜欢
随机推荐
Improve jpopup to realize dynamic control disable
04. 项目博客之日志
jdbc使用call调用存储过程报错
注释、接续、转义等符号
Three. JS learning - light and shadow (understanding)
改善Jpopup以实现动态控制disable
MySQL advanced learning summary 9: create index, delete index, descending index, and hide index
How to download GB files from Google cloud hard disk
Huawei od computer test question 2
Simple understanding of interpreters and compilers
Oracle query table index, unique constraint, field
Pickle and savez_ Compressed compressed volume comparison
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
Game push image / table /cv/nlp, multi-threaded start
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
Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
Vulhub vulnerability recurrence 73_ Webmin
Summary of deep learning tuning tricks
TCP three handshakes you need to know
05. Security of blog project






