当前位置:网站首页>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)
}
}
}
边栏推荐
- 30岁汇源,要换新主人了
- qt creater断点调试程序详解
- Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
- CODING DevSecOps 助力金融企业跑出数字加速度
- Misc Basic test method and knowledge points of CTF
- Common PHP interview questions (1) (written PHP interview questions)
- I spring web upload
- Bugku's Eval
- Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
- Does maxcompute have SQL that can query the current storage capacity (KB) of the table?
猜你喜欢
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
MySQL----函数
I include of spring and Autumn
lv_font_conv离线转换
qt creater断点调试程序详解
MySQL之CRUD
Reasons and solutions for redis cache penetration and cache avalanche
数据库学习——数据库安全性
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
Install and configure Jenkins
随机推荐
Bugku easy_ nbt
Coding devsecops helps financial enterprises run out of digital acceleration
729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
[JVM] operation instruction
Redis' transaction mechanism
P6183 [USACO10MAR] The Rock Game S
Aike AI frontier promotion (7.5)
I spring web upload
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
JMeter performance test: serveragent resource monitoring
Detailed explanation of QT creator breakpoint debugger
Bugku telnet
Easyocr character recognition
百亿按摩仪蓝海,难出巨头
Redis distributed lock principle and its implementation with PHP (2)
MySQL之CRUD
Creation and use of thymeleaf template
P1451 求细胞数量/1329:【例8.2】细胞
想问下大家伙,有无是从腾讯云MYSQL同步到其他地方的呀?腾讯云MySQL存到COS上的binlog
市值蒸发超百亿美元,“全球IoT云平台第一股”赴港求生