js防抖節流
阿新 • • 發佈:2021-01-21
//防抖
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 } } }