# Interval Cancellations (LeetCode #2725)
IntuitionThe goal is to repeatedly execute a given function fn at fixed intervals and provide the ability to cancel the execution.
The immediate thought is to u
## Intuition
The goal is to repeatedly execute a given function `fn` at fixed intervals and provide the ability to cancel the execution.  
The immediate thought is to use JavaScript's built-in `setInterval` to schedule repeated execution and `clearInterval` to stop it. Additionally, the function should execute once immediately upon initialization.

## Approach
1. **Immediate Execution**:
   - Call the function `fn` with the provided arguments `args` as soon as the `cancellable` function is invoked.
   
2. **Scheduling with `setInterval`**:
   - Use `setInterval` to repeatedly call `fn` with the arguments `args` at intervals of `t` milliseconds.
   
3. **Cancellation**:
   - Return a cancellation function that calls `clearInterval` with the interval ID to stop further executions.

4. **Testing**:
   - Use a combination of `setTimeout` to cancel the interval after a specified time and verify the correctness of execution by tracking results in a list with timestamps.

## Complexity
- **Time Complexity**:  
  $$O(n)$$, where \(n\) is the number of times the function is executed before cancellation. Each execution takes constant time \(O(1)\).

- **Space Complexity**:  
  $$O(1)$$, as we only store the interval ID.

## Example Walkthrough

### Input:
- Function: `fn = (x) => x * 2`
- Arguments: `args = [4]`
- Interval: `t = 35ms`
- Cancellation Time: `190ms`

### Execution:
1. At `time = 0ms`, `fn(...args)` is called immediately, returning `8`.
2. At `time = 35ms`, `fn(...args)` is executed again, returning `8`.
3. The pattern continues at `time = 70ms`, `105ms`, `140ms`, `175ms`.
4. At `190ms`, the interval is cancelled, stopping further executions.

### Output:
```json
[
    {"time": 0, "returned": 8},
    {"time": 35, "returned": 8},
    {"time": 70, "returned": 8},
    {"time": 105, "returned": 8},
    {"time": 140, "returned": 8},
    {"time": 175, "returned": 8}
]

# Code
```javascript []
/**
 * @param {Function} fn
 * @param {Array} args
 * @param {number} t
 * @return {Function}
 */
var cancellable = function(fn, args, t) {
    // Set up the interval to execute the function with the given arguments
    fn(...args)
    const intervalId = setInterval(() =>{
        fn(...args);
    },t);

    // return a function to cancel the interval
    return () => clearInterval(intervalId);
};

/**
 *  const result = [];
 *
 *  const fn = (x) => x * 2;=
 *  const args = [4], t = 35, cancelTimeMs = 190;
 *
 *  const start = performance.now();
 *
 *  const log = (...argsArr) => {
 *      const diff = Math.floor(performance.now() - start);
 *      result.push({"time": diff, "returned": fn(...argsArr)});
 *  }
 *       
 *  const cancel = cancellable(log, args, t);
 *
 *  setTimeout(cancel, cancelTimeMs);
 *   
 *  setTimeout(() => {
 *      console.log(result); // [
 *                           //     {"time":0,"returned":8},
 *                           //     {"time":35,"returned":8},
 *                           //     {"time":70,"returned":8},
 *                           //     {"time":105,"returned":8},
 *                           //     {"time":140,"returned":8},
 *                           //     {"time":175,"returned":8}
 *                           // ]
 *  }, cancelTimeMs + t + 15)    
 */
```
['JavaScript']