Timeout Cancellation - Problem
Timeout Cancellation is a fundamental JavaScript concurrency problem that tests your understanding of asynchronous operations and timing control.
You need to create a cancellable timeout system. Given a function
1. Return a cancel function that can stop the execution
2. Schedule
3. If the cancel function is called before
4. If the cancel function is not called, execute
This pattern is commonly used in debouncing, request cancellation, and cleanup operations in real-world applications.
You need to create a cancellable timeout system. Given a function
fn, an array of arguments args, and a timeout delay t in milliseconds, your task is to:1. Return a cancel function that can stop the execution
2. Schedule
fn to execute after t milliseconds3. If the cancel function is called before
t milliseconds, prevent fn from executing4. If the cancel function is not called, execute
fn with the provided argsThis pattern is commonly used in debouncing, request cancellation, and cleanup operations in real-world applications.
Input & Output
example_1.js โ Function Executes (Not Cancelled)
$
Input:
fn = (x) => x * 2, args = [5], t = 1000, cancelTimeMs = 2000
โบ
Output:
Function executes after 1000ms, outputs: 10
๐ก Note:
Since the cancel function is called after 2000ms but the function is scheduled to execute after 1000ms, the function executes normally and returns 10.
example_2.js โ Function Cancelled Before Execution
$
Input:
fn = () => console.log('Hello World'), args = [], t = 1000, cancelTimeMs = 500
โบ
Output:
Function never executes, no output
๐ก Note:
The cancel function is called after 500ms, which is before the scheduled execution time of 1000ms. Therefore, the function is cancelled and never executes.
example_3.js โ Immediate Cancellation Edge Case
$
Input:
fn = (a, b) => a + b, args = [3, 4], t = 100, cancelTimeMs = 0
โบ
Output:
Function cancelled immediately, no output
๐ก Note:
The cancel function is called immediately (0ms), preventing the function from executing even though it has a very short 100ms timeout.
Visualization
Tap to expand
Understanding the Visualization
1
Set Timer
setTimeout schedules function execution and returns a timer ID
2
Store ID
Keep the timer ID as a reference for potential cancellation
3
Wait or Cancel
Either the timer expires naturally or clearTimeout stops it early
4
Outcome
Function executes if timer completes, or gets cancelled if clearTimeout is called
Key Takeaway
๐ฏ Key Insight: JavaScript's setTimeout/clearTimeout provides O(1) timer management - the browser's event loop handles all the timing complexity for us efficiently.
Time & Space Complexity
Time Complexity
O(1)
Both setTimeout and clearTimeout are constant time operations
โ Linear Growth
Space Complexity
O(1)
Only stores a single timeout ID
โ Linear Space
Constraints
- 1 โค t โค 104 (timeout between 1ms and 10 seconds)
- 0 โค cancelTimeMs โค 104 (cancellation can be immediate or delayed)
- args is a valid array that matches fn's parameter signature
- fn is guaranteed to be a valid function
- The environment supports setTimeout and clearTimeout
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code