50 ms Beats 86.01%
IntuitionThe problem requires us to repeatedly execute a function fn at fixed intervals of t milliseconds. Additionally, we need to return a function that can s
# Intuition  
The problem requires us to repeatedly execute a function `fn` at fixed intervals of `t` milliseconds. Additionally, we need to return a function that can stop this repeated execution when called.  

The first thought that comes to mind is to use `setInterval`, which schedules repeated execution of a function at a specified interval. We also need to return a cancel function that uses `clearInterval` to stop the execution.  

---

# Approach  
1. **Execute `fn` immediately**:  
   - The problem states that `fn` should run once **immediately** before setting the interval.  
   - This is done using `fn(...args)`.  

2. **Use `setInterval` for repeated execution**:  
   - We use `setInterval(() => fn(...args), t);` to execute `fn` every `t` milliseconds.  
   - The `setInterval` function returns an `intervalId` that allows us to stop execution later.  

3. **Return a cancel function**:  
   - The cancel function, when called, should stop the repeated execution by calling `clearInterval(intervalId)`.  

---

# Complexity  
- **Time Complexity:** \( O(1) \)  
  - `setInterval` runs asynchronously and does not depend on the number of calls.  
  - The function execution itself takes constant time.  

- **Space Complexity:** \( O(1) \)  
  - We store only a single `intervalId`, which takes constant space.  

---

# Code  
```javascript
/**
 * @param {Function} fn
 * @param {Array} args
 * @param {number} t
 * @return {Function}
 */
var cancellable = function(fn, args, t) {
    // Execute the function immediately once
    fn(...args);

    // Start interval execution
    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); // Expected 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}
 *      // ]
 *  }, cancelTimeMs + t + 15);
 */
```

---

### 🔥 Summary  
- Used `setInterval` for repeated execution.  
- Executed `fn` **once immediately** before starting the interval.  
- Returned a cancel function to stop execution using `clearInterval`.  
- Maintained **O(1) time and space complexity** for efficiency.  

This should work perfectly! 🚀
['JavaScript']