当前位置:网站首页>Throttling anti shake function of JS handwritten function
Throttling anti shake function of JS handwritten function
2022-07-26 04:28:00 【I haven't figured out the name yet*】
One 、 Anti shake function
Anti shake function is one of the best common solutions to save memory and server pressure , Such as Baidu search .
Handwriting thinking : When within the specified time , When triggered again , Will clear the timer , Wait again .
/* Anti shake function */
const debounce =(fn,delay)=>{
let timer = null
return function(){
if(timer) clearTimeout(timer)
timer = setTimeout(()=>{
fn.call(this,...arguments)
},delay)
return [].slice.call(arguments)
}
}
const fun =(a)=>{
console.log(a)
}
const db = debounce(fun,1000)
console.log(db(11),11)
console.log(db(22),22)
console.log(db(33),33)
Two 、 Throttling function
Throttling function is frequently used in buttons , Such as login .
Handwriting thinking : When within the specified time , When triggered again , The timer time will not be recalculated , Only the first time .
onst throttle =(fn,delay)=>{
let timer = null
return function(){
if(!timer){
timer = setTimeout(()=>{
fn.call(this,...arguments)
clearTimeout(timer)
},delay)
}
}
}
const tt = throttle(fun,1000)
console.log(tt(11),11)
console.log(tt(22),22)
console.log(tt(33),33)
边栏推荐
- 【学习笔记】AGC041
- 解决:RuntimeError: Expected object of scalar type Int but got scalar type Double
- ROS2的launch有何不同?
- UE4 键盘控制开关灯
- AWS Support Plan
- Wu Enda's machine learning after class exercises - linear regression
- Build a maker Education Laboratory for teenagers
- How to write abstract in English thesis?
- 二、国际知名项目-HelloWorld
- Acwing_ 12. Find a specific solution for the knapsack problem_ dp
猜你喜欢

1. Excel的IF函数

Sangi diagram of machine learning (for user behavior analysis)

Li Kou daily question - day 42 -661. Picture smoother

数组排序3

The auxiliary role of rational cognitive educational robot in teaching and entertainment

生活相关——十年的职业历程(转)

Support proxy direct connection to Oracle database, jumpserver fortress v2.24.0 release

Tutorial on using the one click upgrade function of the rtsp/onvif protocol video platform easynvr service
远坂凛壁纸

Creative design principle of youth maker Education
随机推荐
Recommendation | scholar's art and Tao: writing papers is a skill
How to choose the key words of the thesis?
Compiled by egg serialize JS
SwiftUI一日速成
华为再发“天才少年”全球召集令,有人曾放弃360万年薪加入
Apisex's exploration in the field of API and microservices
UE4 靠近物体时显示文字,远离时文字消失
1. Mx6u-alpha development board (GPIO interrupt experiment)
Use of anonymous functions
Cnosdb Nirvana Rebirth: abandon go and fully embrace rust
华为高层谈 35 岁危机,程序员如何破年龄之忧?
香甜的黄油
Can literature | relationship research draw causal conclusions
Recommendation Book Educational Psychology: a book for tomorrow's teachers~
九、文件上传和下载
egg-ts-sequelize-CLI
Li Kou daily question - day 42 -661. Picture smoother
[C language foundation] 13 preprocessor
P-norm (2-norm is Euclidean norm)
图论:拓扑排序