Interval Cancellation - Problem
Interval Cancellation is a fundamental JavaScript timing problem that tests your understanding of setInterval(), clearInterval(), and function closures.

You're given a function fn, an array of arguments args, and an interval time t (in milliseconds). Your task is to create a cancellation system that:

1. Immediately calls fn with the provided args
2. Repeatedly calls fn every t milliseconds
3. Returns a cancel function that stops the interval when called

The function should continue executing at regular intervals until the cancel function is invoked. This pattern is commonly used in web development for periodic updates, polling APIs, or creating animated effects that can be stopped on demand.

Example: If fn = (x) => console.log(x), args = ['hello'], and t = 1000, then 'hello' should be logged immediately, then again every 1000ms until cancelled.

Input & Output

example_1.js โ€” Basic Counter
$ Input: fn = (x) => x * 2, args = [4], t = 35, cancelTimeMs = 190
โ€บ Output: [{"time": 0, "returned": 8}, {"time": 35, "returned": 8}, {"time": 70, "returned": 8}, {"time": 105, "returned": 8}, {"time": 140, "returned": 8}, {"time": 175, "returned": 8}]
๐Ÿ’ก Note: The function immediately executes at t=0, then every 35ms until cancelled at 190ms. Each call returns 4 * 2 = 8.
example_2.js โ€” Quick Cancellation
$ Input: fn = (x1, x2) => x1 + x2, args = [2, 5], t = 30, cancelTimeMs = 50
โ€บ Output: [{"time": 0, "returned": 7}, {"time": 30, "returned": 7}]
๐Ÿ’ก Note: Function executes immediately at t=0 returning 2+5=7, then once more at t=30. Cancelled before third execution at t=60.
example_3.js โ€” Immediate Cancellation
$ Input: fn = (a, b, c) => a + b + c, args = [5, 1, 3], t = 50, cancelTimeMs = 25
โ€บ Output: [{"time": 0, "returned": 9}]
๐Ÿ’ก Note: Function executes only once immediately at t=0 returning 5+1+3=9. Cancelled before first interval execution at t=50.

Visualization

Tap to expand
Interval Cancellation TimelineTime โ†’t=0Immediatet=35t=70t=105t=140t=175โœ•Cancelt=190Function Execution Logโœ“ fn(4) โ†’ 8 at t=0ms (immediate)โœ“ fn(4) โ†’ 8 at t=35ms (interval)โœ“ fn(4) โ†’ 8 at t=70msโœ“ fn(4) โ†’ 8 at t=105msโœ“ fn(4) โ†’ 8 at t=140msโœ“ fn(4) โ†’ 8 at t=175msโš ๏ธ Cancelled at t=190msclearInterval() stops future executionsNo more function calls after this pointCode Patternfn(...args); const id = setInterval(() => fn(...args), t);return () => clearInterval(id);
Understanding the Visualization
1
Immediate Execution
Function runs immediately at t=0 when cancellable() is called
2
Interval Setup
setInterval schedules repeated executions every t milliseconds
3
Regular Execution
Function continues to execute at regular intervals
4
Cancellation
When cancel function is called, clearInterval stops all future executions
Key Takeaway
๐ŸŽฏ Key Insight: Use closure to capture the interval ID, enabling the cancel function to stop the recurring execution. The immediate call + setInterval pattern is the standard JavaScript approach for cancellable intervals.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

All operations (function call, setInterval, clearInterval) are constant time

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only stores one interval ID, constant space regardless of how many times function executes

n
2n
โœ“ Linear Space

Constraints

  • fn is a JavaScript function
  • args is a valid JSON array of arguments for fn
  • 1 โ‰ค t โ‰ค 1000 (interval time in milliseconds)
  • 0 โ‰ค cancelTimeMs โ‰ค 1000 (cancellation time in milliseconds)
  • The function must be called immediately, then at regular intervals
  • The returned cancel function must stop all future executions
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28 Netflix 22
42.3K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen