Skip to content

Latest commit

 

History

History
27 lines (19 loc) · 701 Bytes

what_is_throttling_.md

File metadata and controls

27 lines (19 loc) · 701 Bytes

What is throttling?

Throttling ensures that a function is called at most once in a specified time interval. It is useful for rate-limiting events like scrolling or resizing.

Example:

function throttle(func, limit) {
  let inThrottle;
  return function(...args) {
    if (!inThrottle) {
      func.apply(this, args);
      inThrottle = true;
      setTimeout(() => (inThrottle = false), limit);
    }
  };
}

const log = throttle(() => console.log('Throttled!'), 1000);
window.addEventListener('scroll', log);

Tags: intermediate, JavaScript, Performance