[Explanation] Very simple solution using ClearTimeout⌚| 56 ms - Beats 91.67%
CodeExplanationProblem DescriptionYou're given:

A functionfn.


An array of argumentsargsfor the functionfn.


A delay timetin milliseconds.

Your task is to r


# Code
```javascript []
var cancellable = function(fn, args, t) {
    const timeout = setTimeout(() => {
        fn(...args)
    }, t)
    return function (){
        clearTimeout(timeout)
    }
};
```
# Explanation

### Problem Description

You're given:

1.  A function `fn`.
    
2.  An array of arguments `args` for the function `fn`.
    
3.  A delay time `t` in milliseconds.
    

Your task is to return a cancellation function `cancelFn` that:

1.  Initially delays the execution of `fn` by `t` milliseconds.
    
2.  If `cancelFn` is invoked before the `t` milliseconds delay, it cancels the execution of `fn`.
    
3.  If `cancelFn` is not invoked within `t` milliseconds, `fn` is executed with `args`.
    

### Code Explanation

Here’s the code :

```javascript []
var cancellable = function(fn, args, t) {
    const timeout = setTimeout(() => {
        fn(...args)
    }, t)
    return function () {
        clearTimeout(timeout)
    }
};

```

-   **setTimeout**: This function sets a timer that executes `fn` after `t` milliseconds with the provided arguments `args`.
    
-   **clearTimeout**: This function cancels the timer set by `setTimeout` if called before the delay `t`.
    

### How it Works

1.  **Setting the Timeout**: The function `fn` is scheduled to run after `t` milliseconds by using `setTimeout`.
    
2.  **Returning Cancel Function**: The `cancellable` function returns a new function `cancelFn`.
    
3.  **Canceling Execution**: When `cancelFn` is called, it uses `clearTimeout` to cancel the previously scheduled execution of `fn`.
    

### Examples

1.  **Example 1**:
    
    -   `fn = (x) => x * 5`, `args = [2]`, `t = 20`
        
    -   `const cancelTimeMs = 50`
        
    -   `cancelFn` is scheduled after 50ms, which is after `fn` would have executed.
        
    -   So, `fn(2)` executes at 20ms, returning `10`.
        
2.  **Example 2**:
    
    -   `fn = (x) => x**2`, `args = [2]`, `t = 100`
        
    -   `const cancelTimeMs = 50`
        
    -   `cancelFn` is scheduled after 50ms, which is before `fn` would have executed.
        
    -   `fn(2)` never executes, so the output is an empty array.
        
3.  **Example 3**:
    
    -   `fn = (x1, x2) => x1 * x2`, `args = [2, 4]`, `t = 30`
        
    -   `const cancelTimeMs = 100`
        
    -   `cancelFn` is scheduled after 100ms, which is after `fn` would have executed.
        
    -   So, `fn(2, 4)` executes at 30ms, returning `8`.
        

### Summary

The `cancellable` function ensures that `fn` will be executed after `t` milliseconds unless `cancelFn` is called before `t` milliseconds elapses, cancelling the execution.
['JavaScript']