HappyJayXin
1/20/2020 - 3:17 AM

JavaScript throttle

JavaScript throttle

function throttle(func, delay){
  let inThrottle;
  let timeout = null;
  
  return function(){
    let context = this;
    let args = arguments;
    if(!inThrottle){
      func.apply(context, args)
      inThrottle = true;
      clearTimeout(timeout);
      timeout = setTimeout(function(){
        inThrottle = false
      }, delay)
    }
  }
}