当前位置:网站首页>Dry goods -- encapsulated anti shake and throttling method in the project
Dry goods -- encapsulated anti shake and throttling method in the project
2022-07-28 13:21:00 【CCC Chen Chen】
Why do I need anti shake throttling :
It is often easy to encounter some continuous events in development , such as scroll, resize, input, mousemove etc. , Execute callback frequently , Cause loss of page performance , The page is easy to get stuck , At this time, there are two solutions , Anti shake and backflow
Shake proof :
After the event is triggered time Execute only once in time . The principle is to maintain a time delayer , It is stipulated in time Execute the function after time , If in time Trigger again in time , Then cancel the previous time delayer reset . So the callback is executed only once .
clearTimeout() Method
Cancelable by setTimeout() Method set timeout
grammar :clearTimeout(id_of_settimeout)
id_of_settimeout: from setTimeout() Back to ID value . This value identifies the block of deferred execution code to cancel .
function debounce(interval = 400, immediate = false) {
let timer
return function (fn) {
if (immediate) {
immediate = false
fn && fn()
return
}
window.clearTimeout(timer)
timer = window.setTimeout(() => {
window.clearTimeout(timer)
fn && fn()
}, interval)
}
}
Page call :
** quote :**
mounted() {
this.debounceHandler = debounce(400, false)
},
** Specific use in the project :**
this.debounceHandler(() => {
...
})
// This effect is triggered by the event 400ms The latter function is executed only once
throttle :
During event triggering , every other time Perform a callback
function throttle(interval = 400) {
let lastTime = 0
return function (fn) {
const currentTime = new Date().getTime()
if (currentTime - lastTime >= interval) {
lastTime = currentTime
fn && fn()
}
}
}
边栏推荐
- Extended operator
- Sub thread update UI full solution
- Automatic light sensing arm lamp touch chip-dlt8sa15b-jericho
- Transaction of MySQL underlying principle (2)
- BiliBili Yang Zhou: above efficiency, efficient delivery
- 9、 Kubernetes configuration and storage
- How to design a second kill system?
- Getderivedstatefromprops lifecycle
- Rust 从入门到精通01-简介
- GameStop熊市杀入NFT交易,老牌游戏零售商借Web3焕发第二春
猜你喜欢

leetcdoe-342. 4的幂

PCP parity principle arbitrage

Complete set of SSM framework online bookstore

为什么说Crypto游戏正在改变游戏产业?

How to design a second kill system?

Chinese translation of pointnet:deep learning on point sets for 3D classification and segmentation

Low code: reduce technical capability requirements and improve software development efficiency

Analysis of Andriod low on memory printing principle

Rust from introduction to mastery 01 introduction

黑猫带你学eMMC协议第24篇:eMMC的总线测试程序详解(CMD19 & CMD14)
随机推荐
PCP parity principle arbitrage
Ruan Bonan of Green Alliance Technology: cloud native security from the open source shooting range
Shell基础概念和变量
Leetcode 笔记 118. 杨辉三角
[embedded C foundation] Part 3: constants and variables
【嵌入式C基础】第1篇:基本数据类型
Shenwenbo, researcher of the Hundred Talents Program of Zhejiang University: kernel security in the container scenario
gicv3 spi register
企业数字化本质
ES6 null merge operator (?)
Table list filter results remain unchanged
How does the vditor renderer achieve server-side rendering (SSR)?
我抄底了被清算的NFT,却被OpenSea上了锁
Fast classification of array.group() in ES6
The difference between sessionstorage, localstorage and cookies
Remove the plug-in of category in WordPress link
leetcdoe-342. 4的幂
[July 5 event preview] Flink Summit
[FPGA]: AD sampling problem
Original juice multifunctional Juicer touch chip-dlt8t02s-jericho