函数节流与函数防抖

函数节流是固定的时间段内只能执行一次

函数防抖是固定的时间段内反复触发只有最后一次生效

function debound(callback,delay){

var t=null;
return function(){
  clearTimeout(t);
  t=setTimeout(callback,delay)
}
} window.onscroll
=debounce(function(){

console.log('调用了1次')

},500)

 函数节流 固定的时间段内只能执行一次

function throttle(callback,duration){
 var lasttime=new Date().getTime();

return function(){
  var now=new Date.getTime()
  if(now-lasttime>duration){
   lasttime=now;
   callback()
}

}

}

window.onscroll=throttle(function{
   console.log(‘调用了1次’)

},500)
 
原文地址:https://www.cnblogs.com/h5it/p/12765267.html