当前位置:网站首页>Throttling and anti shake

Throttling and anti shake

2022-06-24 00:01:00 qq_ fifty-three million two hundred and eighty thousand eight h

  1. The purpose of throttling and anti shake :
    To limit the frequency of function execution , The trigger frequency of the optimization function is too high, so the response speed can not keep up with the trigger frequency , Prevent delay caused by frequently triggering the same event in a short time , The phenomenon of feigning death or being stuck .

  2. The difference between throttling and anti shake :
    throttle : There is an incident A Timer set , So in delay Trigger before , Will only trigger once .

Shake proof : If in delay Re trigger before , Then the timer will continue to re time , Finally, it will be executed after the last time , For those requiring real-time response , Throttling should be used .
3: Example

//  throttle `

function throttle(fun,delay){
    
	let flag = true;
	return function(){
    
	if(!flag){
    
	return;
}
flag = false;
setTimeout(()=>{
    
	fun.apply(this,arguments);
	flag = true;
},delay)
}
}

function output(){
    

console.log(' throttle ',Math.random());

}





//  Shake proof 

function antiShake(fun,delay){
    
	let timeout = null;
	return function(e){
    
	clearTimeout(timeout);
	timeout = setTimeout(()=>{
    
	fun.apply(this,argument);

},delay)
}
}
function play(){
    
	console.log(' Shake proof ');
}
原网站

版权声明
本文为[qq_ fifty-three million two hundred and eighty thousand eight h]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206232119547252.html