当前位置:网站首页>Anti shake and throttling

Anti shake and throttling

2022-04-23 08:05:00 Pen drawing Acacia

Shake proof : The problem to be solved is when an event is triggered multiple times within a time interval , It's only going to be executed once
throttle : The solution is to reduce the trigger frequency for a period of time
1: The function will only trigger once in a specified time , Specific implementation method
When the function is triggered for the first time , Delay delay Time execution , If in delay The function is triggered again within a time period , Then start over
timing
If delay The function is not triggered within the time period , Then execute the function

	function bunde(fn ,depaly)
		{
			let timetar=null;
			return function()
			{
				if(timetar)
				{
					clearTimeout(timetar);
				}
				timetar=setTimeout(fn,depaly);
			}
		}

throttle :
The problem with anti shake is , Continuously trigger events in a short time , Callback functions never execute .
The idea of saving money : Continuously trigger events in a short time , The callback function will only execute within the specified interval .

	function throttle(fn, delay) {
				let timer = null;
				return function() {
					if (timer) return false
					 timer = setTimeout(() => {
						fn()
					   timer = null
					}, delay)
				}
			}

 Insert picture description here

版权声明
本文为[Pen drawing Acacia]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230624332804.html