当前位置:网站首页>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)
}
}
}边栏推荐
- Interpretation of Apache linkage parameters in computing middleware
- Cartoon: what are the attributes of a good programmer?
- Run faster with go: use golang to serve machine learning
- Ionic Cordova project modification plug-in
- MySQL----函数
- lv_font_conv离线转换
- JS bright blind your eyes date selector
- 机器学习笔记 - 灰狼优化
- Hongmeng system -- Analysis from the perspective of business
- Stm32+bh1750 photosensitive sensor obtains light intensity
猜你喜欢

Number protection AXB function! (essence)

I spring web upload

729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "

Selection and use of bceloss, crossentropyloss, sigmoid, etc. in pytorch classification

Bugku's steganography

Crud of MySQL

Select sort and bubble sort
![1330: [example 8.3] minimum steps](/img/69/9cb13ac4f47979b498fa2254894ed1.gif)
1330: [example 8.3] minimum steps

华为哈勃化身硬科技IPO收割机

Bugku's Ah Da
随机推荐
数据库学习——数据库安全性
B站做短视频,学抖音死,学YouTube生?
lvgl 显示图片示例
Fr exercise topic --- comprehensive question
Bugku's Ah Da
12 MySQL interview questions that you must chew through to enter Alibaba
Coding devsecops helps financial enterprises run out of digital acceleration
Run faster with go: use golang to serve machine learning
CODING DevSecOps 助力金融企业跑出数字加速度
做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
Handwriting promise and async await
Bugku cyberpunk
美团优选管理层变动:老将刘薇调岗,前阿里高管加盟
MongDB学习笔记
The elimination strategy of redis
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
可转债打新在哪里操作开户是更安全可靠的呢
亿咖通科技通过ISO27001与ISO21434安全管理体系认证
超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜
市值蒸发超百亿美元,“全球IoT云平台第一股”赴港求生