JavaScript Course
JavaScript
/
Advanced

Implement: Throttle

Definition

Write a function `throttle(func, limit)` that ensures the given function is called at most once in a specified time period.

Explain Like I'm New

Throttle is a roller coaster. The ride takes exactly 3 minutes. If you try to get on the ride while it's moving, you are ignored. You can only get on once the 3 minutes are up and the ride returns.

Real World Example

Scroll event listeners. If you attach an animation to `window.onscroll`, it will fire 100 times a second, crashing the browser. Throttling it to 50ms ensures the animation only updates 20 times a second, keeping it smooth.

Common Use Cases

  • •Scroll events
  • •Drag and drop
  • •Mouse move events

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • When is Throttle better than Debounce?

intermediate

  • Write a basic Throttle implementation.

advanced

  • What is the 'trailing edge' in Throttle implementations?

Flash Cards

Question

When is Throttle better?

Click to reveal answer
Answer

When you need continuous visual updates DURING the action. Debouncing a scroll listener means the UI won't update until the user completely STOPS scrolling. Throttling means the UI updates smoothly while they are actively scrolling.