Course contentsShow
← JavaScript
Lesson 2387 of 2,72349. Common Patterns and IdiomsFree lesson

What Throttle Solves

Understand throttle's role in ensuring a function runs at most once per time interval.

What Throttle Solves

You'll learn: how throttling ensures a function executes at most once per fixed time interval, preventing performance bottlenecks from rapid, repeated calls.

The Problem Throttle Addresses

Some events fire constantly — scrolling, mouse movement, window resizing. If you attach a heavy operation (like re-rendering UI, recalculating layouts, or making API calls) directly to these events, your function might execute dozens or even hundreds of times per second, freezing the browser.

Debounce waits until activity stops. But what if you want updates during the activity, just not on every single event? That's where throttle comes in.

How Throttle Works

Throttle ensures your function runs at most once per time interval. For example, with a 200ms throttle:

  • The function runs immediately on the first call
  • Any calls within the next 200ms are ignored
  • After 200ms, the next call is allowed

Think of it like a rate limiter: "You can call this function, but only once every X milliseconds."

Real-World Analogy

Imagine a security guard checking IDs at a club entrance. They let one person in, then must wait 5 seconds before checking the next person — no matter how many people line up during those 5 seconds. The line might grow, but the guard processes entries at a steady, controlled rate.

When You Need Throttle

  • Scroll handlers: Update a "scroll progress" indicator every 100ms, not every pixel
  • Resize handlers: Recalculate layout at intervals, not continuously
  • API calls on input: Send search requests every 300ms while typing, preventing request spam
  • Game loops or animations: Ensure frame rate consistency

Key Takeaway: Throttle limits function execution frequency to fixed intervals, allowing ongoing updates during continuous activity while preventing performance collapse from excessive calls.