Timeout Cancellation
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript []
function cancellable(fn, args, t) {
    // Timeout ID for the function call
    let timeoutId;

    // The cancel function
    const cancelFn = () => {
        clearTimeout(timeoutId); // Clear the timeout if cancelFn is called before fn executes
    };

    
    timeoutId = setTimeout(() => {
        fn(...args); 
    }, t);

    
    return cancelFn;
}


const fn1 = (x) => x * 5;
const cancelFn1 = cancellable(fn1, [2], 20);
setTimeout(cancelFn1, 50); 
const fn2 = (x) => x ** 2;
const cancelFn2 = cancellable(fn2, [2], 100);
setTimeout(cancelFn2, 50); 

const fn3 = (x1, x2) => x1 * x2;
const cancelFn3 = cancellable(fn3, [2, 4], 30);
setTimeout(cancelFn3, 100); 

```
['JavaScript']