LeetCode

2715. Timeout Cancellation

Easy
July 17, 2025 · Asynchronous · Timers · leetcode.com

Problem

Given a function fn, an array of arguments args, and a timeout t in milliseconds, return a cancelFn function.

The function fn should be executed after t milliseconds with the arguments from args. However, if the returned cancelFn is invoked before the timeout ends, fn should not be executed.

Example 1:

Input: (fn = (x) => x * 5), (args = [2]), (t = 20);
Output: [{ time: 20, returned: 10 }];

Example 2:

Input: (fn = (x) => x ** 2), (args = [2]), (t = 100);
Output: [];

My Approach

Initial Thoughts

This is essentially a timing and cancellation problem. We are dealing with delayed execution and possible interruption before the scheduled time.

Solution Strategy

  1. Immediately schedule fn(...args) using setTimeout
  2. Capture the timeout ID so we can cancel it later
  3. Return a cancelFn that can call clearTimeout(timeoutId)
  4. If cancelFn is called before t ms, fn will never run
  5. Otherwise, fn executes after t ms as expected

Solution

JavaScript one-liner with closure:

var cancellable = function (fn, args, t) {
  let timeoutID = setTimeout(() => fn(...args), t);
  return () => clearTimeout(timeoutID);
};

Complexity Analysis

Measure Complexity
Time Complexity O(1)
Space Complexity O(1)

Key Insights & Learnings

  1. Closures are essential: The returned cancel function has access to timeoutID because it’s enclosed within the parent scope.
  2. setTimeout returns a handle: This ID is required for canceling the timeout.
  3. Immediate scheduling: The setTimeout should run immediately when cancellable is called, not when cancelFn is called.
  4. Timing precision: The actual execution time may vary slightly due to the JS event loop, so always use performance.now() or Date.now() when testing precision.

Common Pitfalls

Use Case / When to Use

Notes

Time Taken: ~5 min Confidence Level: Solid What I’d Do Differently:

Follow-up Questions

  1. What if we wanted cancelFn to return a boolean indicating whether the cancellation was successful?
  2. How could we log whether fn was actually executed?
  3. Could this be refactored to support multiple scheduled executions with separate cancels?

This problem reinforced my understanding of how closures and timers work in JavaScript, especially for delayed and cancellable executions.