Interval Cancellation - Problem
Interval Cancellation is a fundamental JavaScript timing problem that tests your understanding of
You're given a function
1. Immediately calls
2. Repeatedly calls
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
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 args2. Repeatedly calls
fn every t milliseconds3. 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
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
โ Linear Growth
Space Complexity
O(1)
Only stores one interval ID, constant space regardless of how many times function executes
โ Linear Space
Constraints
-
fnis a JavaScript function -
argsis a valid JSON array of arguments forfn -
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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code