Skip to content
Onsite

Clear All Timeouts

MediumP3Coding

Implement a pair of helpers that let you cancel every pending timeout at once.

  • trackedSetTimeout(fn, delay, ...args) works like setTimeout (same arguments, returns the timer id) but remembers the timer while it is pending.
  • clearAllTimeouts() cancels every timer created by trackedSetTimeout that has not fired yet.

Timers that already fired are not affected, and they must be forgotten once they fire so the tracking list does not grow forever. Timers created after clearAllTimeouts() work normally and are tracked again. A timer id returned by trackedSetTimeout can still be cancelled individually with the normal clearTimeout.

Example 1

Input
trackedSetTimeout(a, 100); trackedSetTimeout(b, 200); clearAllTimeouts();
Output
neither a nor b ever runs

Constraints

  • At most 10^4 pending timers.

Hints

Run checks the 3 visible tests, Submit all 8.