当前位置:网站首页>[interview question] anti shake and throttling
[interview question] anti shake and throttling
2022-07-28 05:49:00 【Tree_ wws】
【 Interview questions 】 Anti shake and throttling
List of articles
1. Anti shake and throttling
Concept
Shake proof : At certain intervals , Turn multiple triggers into one trigger , Similar to the return to the city in the game , You have been triggering the event of returning to the city , But in the end, it can only be executed once , As soon as it's time to return to the city, I'll go home
throttle : Reduce the frequency over a period of time , Similar to the skills in the game CD, You can only use the skill once in the specified time
1. Shake proof
1.1 Ordinary anti shake
/* Anti shake function Parameter one : You need the anti shake function Parameter two : Delay Time */
function debounce (fun,delay){
let timeout = null
return function(){
// If there is a value, clear the timer
if(timeout)
{
clearTimeout(timeout)
}
// If there is no value, turn on a timer
timeout = setTimeout(() => {
/* 1. Here, the arrow function will this Point to the outside 2. Supplementary information arguments( Argument list ) , Each argument The list corresponds to its own internal function , Only inside the function will there be Argument list , Use the arrow function here , Point the arrow inside the function arguments, Point to the outside */
fun.apply(this,arguments)
}, delay);
}
}
function request(e){
console.log(' Send a request ');
console.log(this);
/* e When did it come in ? When window When calling this function, pass in the arguments */
console.log(e);
}
let btn = document.querySelector('button')
btn.addEventListener('click',debounce(request,500))
1.2 Anti shake upgrade
/* Anti shake function Parameter one : You need the anti shake function Parameter two : Delay Time */
function debounce (fun,delay){
let timeout = null
return function(){
// If there is a value, clear the timer
// We need to pay attention to : Clear timer , It doesn't mean that the value is cleared
if(timeout)
{
clearTimeout(timeout)
}else{
// Execute immediately for the first time , The rear trigger effect is the same as that of ordinary anti shake
fun.apply(this,arguments)
}
// If there is no value, turn on a timer
timeout = setTimeout(() => {
/* 1. Here, the arrow function will this Point to the outside 2. Supplementary information arguments( Argument list ) , Each argument The list corresponds to its own internal function , Only inside the function will there be Argument list , Use the arrow function here , Point the arrow inside the function arguments, Point to the outside */
fun.apply(this,arguments)
}, delay);
}
}
function request(e){
console.log(' Send a request ');
console.log(this);
/* e When did it come in ? When window When calling this function, pass in the arguments */
console.log(e);
}
let btn = document.querySelector('button')
btn.addEventListener('click',debounce(request,1000))
1.3 Add anti shake parameters ( Execute now )
/* Anti shake function Parameter one : You need the anti shake function Parameter two : Delay Time Parameter 3 : Whether to enable immediate execution Be careful : Immediate execution here refers to the first click No delay , The first time here refers to after each delay Clicking again is the first time */
function debounce (fun,delay,immediate=false){
let timeout = null
return function(){
// If there is a value, clear the timer
// We need to pay attention to : Clear timer , It doesn't mean that the value is cleared
if(timeout)
{
clearTimeout(timeout)
}
// Determine whether to start immediate execution
if(immediate)
{
// First time in timeout The value is null, Take the inverse as true
let first = !timeout
if(first)
{
fun.apply(this,arguments)
}
timeout = setTimeout(() => {
// Recover after delay timeout Value , Let it call functions
timeout = null
}, delay);
}else
{
// If there is no value, turn on a timer
timeout = setTimeout(() => {
/* 1. Here, the arrow function will this Point to the outside 2. Supplementary information arguments( Argument list ) , Each argument The list corresponds to its own internal function , Only inside the function will there be Argument list , Use the arrow function here , Point the arrow inside the function arguments, Point to the outside */
fun.apply(this,arguments)
}, delay);
}
}
}
function request(e){
console.log(' Send a request ');
console.log(this);
/* e When did it come in ? When window When calling this function, pass in the arguments */
console.log(e);
}
let btn = document.querySelector('button')
btn.addEventListener('click',debounce(request,1000,true))
2. throttle
2.1 The first way to write it
function throttle (fun,delay){
let previous = 0
return function(){
let current = new Date()
// Time greater than delay , It means skills CD It's ready to use
console.log(current - previous);
if(current - previous > delay)
{
fun.apply(this,arguments)
// Update time
previous = current
}
}
}
function request(e){
console.log(' Send a request ');
console.log(this);
/* e When did it come in ? When window When calling this function, pass in the arguments */
console.log(e);
}
let btn = document.querySelector('button')
btn.addEventListener('click',throttle(request,1000))
2.2 The second way
function throttle (fun,delay){
let flag = true
return function(){
if(flag)
{
fun.apply(this,arguments)
flag = false
setTimeout(() => {
// Once the delay time is up, restore the right to use skills
flag = true
}, delay);
}
}
}
function request(e){
console.log(' Send a request ');
console.log(this);
/* e When did it come in ? When window When calling this function, pass in the arguments */
console.log(e);
}
let btn = document.querySelector('button')
btn.addEventListener('click',throttle(request,2000))
summary
That's what we're going to talk about today , I hope that's helpful !!!
边栏推荐
猜你喜欢

media-搭建直播服务器

Review of metallurgical physical chemistry -- Fundamentals of metallurgical reaction kinetics and characteristics of multiphase reaction kinetics

基于Highcharts平台的桑基图(Sankey diagram)绘制

uniapp uview组件库的使用方法(下载方式)

ArcGIS地图制作的注记、格网添加

基于XMind的E-R图制作

单调队列,洛谷P1886 滑动窗口

链表中关于快慢指针的oj题

Microsoft Edge浏览器插件(2)

顺序表的增删查改
随机推荐
Shell operation principle
Annotation and grid addition of ArcGIS map making
DOM基础
树莓派蓝牙调试过程
Problem set in the project
Sequence table OJ topic
A file upload tool website written by individuals
正则表达式
抖音-视频步骤
链表中关于快慢指针的oj题
DOM--事件链、事件冒泡和捕获、事件代理
蓝桥杯 方格填数
Help document making based on easy CHM and vs
书籍-穷查理宝典
标准C语言学习总结8
DOM——事件
C语言回顾(可变参数篇)
浅谈一段奇妙旅程
Zotero - a document management tool
js中==和===区别