1. 程式人生 > 其它 >js防抖節流

js防抖節流

技術標籤:前端html

//防抖
this.debounce((ff)=>{
				console.log('ff'+ff)
				setInterval(()=>{
//節流
					this.throttle((kk)=>{
						console.log(kk)
					},500)(44)
				},900)
				
			},200)(3)

debounce(fn,delay){
				var timer=null
				var that=this
				return function(){
					clearTimeout(timer)
						var argus=Array.prototype.slice.call(arguments)
					timer=setTimeout(function(){
						//console.log(arguments)
						fn.call(that,argus)
					},delay)
				}
			},
			throttle(fn,delay){
				var timer=null;
				var that=this
				var startTimer=0
				return function(){
					var argus=[].slice.apply(arguments)
					var endTimer=new Date().getTime()
					var oddTimer=endTimer-startTimer
					console.log(oddTimer)
					if(oddTimer>delay){
						fn.apply(that,argus)
						startTimer=endTimer
					}
				}
			}