Skip to solution
mediumFrontend

Implement throttle(func, wait, {leading, trailing})

1.0k views
01

Understand the problem

Create throttle that invokes func at most once per wait ms. Support leading/trailing options, plus cancel/flush.

throttletimersleading-trailing
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Approach: Track lastCall time + trailing timer. Leading invokes immediately if elapsed >= wait; trailing schedules last args.

function throttle(func, wait, { leading = true, trailing = true } = {}) {
  let lastCall = 0, timer = null, lastArgs, lastThis, result;
  function invoke(time) {
    lastCal

Solution ready — 2 min read

Classified // press E to declassify

04

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 115 of 190 decoded in the JavaScript Coding track. One more won't hurt.

Back to track