当前位置:网站首页>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)
}
}
}边栏推荐
- Two Bi development, more than 3000 reports? How to do it?
- Ctfshow web entry command execution
- JS bright blind your eyes date selector
- Garbage collection mechanism of PHP (theoretical questions of PHP interview)
- Can I pass the PMP Exam in 20 days?
- Bugku telnet
- Common MySQL interview questions (1) (written MySQL interview questions)
- Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
- I spring web upload
- lvgl 显示图片示例
猜你喜欢

Install and configure Jenkins

Stop B makes short videos, learns Tiktok to die, learns YouTube to live?

Run faster with go: use golang to serve machine learning

No one consults when doing research and does not communicate with students. UNC assistant professor has a two-year history of teaching struggle

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

Bugku telnet

Huawei Hubble incarnation hard technology IPO harvester

Common PHP interview questions (1) (written PHP interview questions)

P1451 求细胞数量/1329:【例8.2】细胞

I spring web upload
随机推荐
想问下大家伙,有无是从腾讯云MYSQL同步到其他地方的呀?腾讯云MySQL存到COS上的binlog
市值蒸发超百亿美元,“全球IoT云平台第一股”赴港求生
Ctfshow web entry command execution
Go learning ----- relevant knowledge of JWT
机器学习笔记 - 灰狼优化
Leetcode: Shortest Word Distance II
Reconnaissance des caractères easycr
Fr exercise topic --- comprehensive question
What are the domestic formal futures company platforms in 2022? How about founder metaphase? Is it safe and reliable?
go学习 ------jwt的相关知识
Interpretation of Apache linkage parameters in computing middleware
华为哈勃化身硬科技IPO收割机
Bugku's eyes are not real
漫画:优秀的程序员具备哪些属性?
漫画:程序员不是修电脑的!
Two Bi development, more than 3000 reports? How to do it?
Common interview questions about swoole
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
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
Coding devsecops helps financial enterprises run out of digital acceleration