当前位置:网站首页>Anti shake and throttling
Anti shake and throttling
2022-07-05 15:21:00 【Shepherd Wolf】
scene ( Frequently triggered events )
Page scroll bar scroll event , Web window resize event ,
Mouse drag move event , Click the button frequently click event ,
input Quickly enter events ......
Shake proof
When the event is triggered continuously , No more triggers for a certain period of time , The event handler will only execute once , If the set time comes before , It triggered the event again , Start the delay again .

Arrow function writing :
function debounce(func, delay) {
var timer = 0
return function(e) {
clearTimeout(timer)
timer = setTimeout(() => {
func(e)
}, delay)
}
}apply change this How to write it :
function debounce(func, delay) {
var timer
return function(e) {
// console.log('timer', timer, e.target.value)
clearTimeout(timer)
var that = this
var args = arguments
timer = setTimeout(function(){
func.apply(that, args);
},delay)
}
}demo
1. Input box :
var validate = debounce(function(e) {
console.log('-- Interface --', e.target.value)
}, 1000)
document.querySelector("input").addEventListener('input', validate)2. Scroll bar :
function showTop () {
var scrollTop = document.body.scrollTop || document.documentElement.scrollTop
console.log(scrollTop)
}
window.onscroll = debounce(showTop, 1000)throttle
When the event is triggered continuously , Make sure to call the event handler only once in a certain period of time .

function throttling(fn, delay){
let timer
return function () {
if (!timer) {
timer = setTimeout(() => {
fn()
clearTimeout(timer)
timer = null
}, delay)
}
}
}边栏推荐
- ionic cordova项目修改插件
- Machine learning notes - gray wolf optimization
- Go learning ----- relevant knowledge of JWT
- Huiyuan, 30, is going to have a new owner
- 超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜
- queryRunner. Query method
- Does maxcompute have SQL that can query the current storage capacity (KB) of the table?
- 机器学习笔记 - 灰狼优化
- Common MySQL interview questions (1) (written MySQL interview questions)
- Fr exercise topic - simple question
猜你喜欢

MySQL之CRUD

Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)

Fr exercise topic - simple question

Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking

Garbage collection mechanism of PHP (theoretical questions of PHP interview)

Bugku's Ah Da

数据库学习——数据库安全性

Fr exercise topic --- comprehensive question

CODING DevSecOps 助力金融企业跑出数字加速度
![[JVM] operation instruction](/img/f5/85580495474ef58eafbb421338e93f.png)
[JVM] operation instruction
随机推荐
Can I pass the PMP Exam in 20 days?
Install and configure Jenkins
Bugku's eyes are not real
Run faster with go: use golang to serve machine learning
计算中间件 Apache Linkis参数解读
Number protection AXB function! (essence)
Anaconda uses China University of science and technology source
Au - delà du PARM! La maîtrise de l'Université de Pékin propose diverse pour actualiser complètement le classement du raisonnement du NLP
P1451 calculate the number of cells / 1329: [example 8.2] cells
[recruitment position] infrastructure software developer
SQL Server learning notes
I spring and autumn blasting-2
MySQL----函数
Garbage collection mechanism of PHP (theoretical questions of PHP interview)
Easyocr character recognition
Common PHP interview questions (1) (written PHP interview questions)
Bugku easy_ nbt
当代人的水焦虑:好水究竟在哪里?
GPS原始坐标转百度地图坐标(纯C代码)
超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜