[Explained] Very Simple solution using Interval methods ⌚| 54 ms - Beats 95.79%
CodeExplanationThe goal is to create a functioncancellablethat takes in a functionfn, an array of argumentsargs, and an interval timet(in milliseconds). The fun


# Code
```javascript []
var cancellable = function(fn, args, t) {
    const myInterval = setInterval(() => {
        fn(...args)
    }, t);

    fn(...args);

    return function() {
        clearInterval(myInterval);
    }
};
```

# Explanation 


The goal is to create a function `cancellable` that takes in a function `fn`, an array of arguments `args`, and an interval time `t` (in milliseconds). The function `cancellable` should return another function `cancelFn` that stops the repeated execution of `fn` after a specified delay (cancelTimeMs). Here's a step-by-step explanation:

1.  **Function Definition:**
    
    
    
    ```javascript[]
    var cancellable = function(fn, args, t) {
    
    ```
    
2.  **Setting Up the Interval:**
    
    -   `setInterval` is used to call the function `fn` with the provided arguments `args` every `t` milliseconds.
        
    
    
    
    ```javascript[]
    const myInterval = setInterval(() => {
        fn(...args)
    }, t);
    
    ```
    
3.  **Immediate Execution:**
    
    -   The function `fn` is called immediately with the provided arguments `args` once before setting up the interval.
        
    
  
    
    ```javascript[]
    fn(...args);
    
    ```
    
4.  **Returning the Cancel Function:**
    
    -   A function is returned that clears the interval, effectively stopping the repeated execution of `fn` when called.
        
    
    ```javascript[]
    return function() {
        clearInterval(myInterval);
    }
    
    ```
    
5.  **Example 1 Explanation:**
    
    -   `fn = (x) => x * 2`, `args = [4]`, `t = 35`
        
    -   `cancelTimeMs = 190`
        
    -   `setTimeout(cancelFn, cancelTimeMs);`
        
    -   Every 35ms, `fn(4)` is called, which returns 8. This repeats until 190ms, then `cancelFn` is called to stop it.
        
    -   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}
        ]       
    
6.  **Example 2 Explanation:**
    
    -   `fn = (x1, x2) => (x1 * x2)`, `args = [2, 5]`, `t = 30`
        
    -   `cancelTimeMs = 165`
        
    -   `setTimeout(cancelFn, cancelTimeMs);`
        
    -   Every 30ms, `fn(2, 5)` is called, which returns 10. This repeats until 165ms, then `cancelFn` is called to stop it.
        
    -   Output:
        
       ``` json []
        [
           {"time": 0, "returned": 10},
           {"time": 30, "returned": 10},
           {"time": 60, "returned": 10},
           {"time": 90, "returned": 10},
           {"time": 120, "returned": 10},
           {"time": 150, "returned": 10}
        ]
7.  **Example 3 Explanation:**
    
    -   `fn = (x1, x2, x3) => (x1 + x2 + x3)`, `args = [5, 1, 3]`, `t = 50`
        
    -   `cancelTimeMs = 180`
        
    -   `setTimeout(cancelFn, cancelTimeMs);`
        
    -   Every 50ms, `fn(5, 1, 3)` is called, which returns 9. This repeats until 180ms, then `cancelFn` is called to stop it.
        
    -   Output:
        
        ```json []
        [
           {"time": 0, "returned": 9},
           {"time": 50, "returned": 9},
           {"time": 100, "returned": 9},
           {"time": 150, "returned": 9}
        ]
        
        ```
        

So in summary, the `cancellable` function sets up repeated calls to `fn` with the provided arguments every `t` milliseconds. The returned `cancelFn` is used to stop these repeated calls after a specified delay.


['JavaScript']