Interval 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) {
    let time = 0; // Track the time of each function call
    let intervalId;
    const results = []; // To store the results of each call

    // Function to handle the repeated execution
    const intervalFn = () => {
        results.push({ time, returned: fn(...args) });
        time += t; // Increment the time for the next interval
    };

    // Call the function immediately
    intervalFn();

    // Set the interval to call the function every t milliseconds
    intervalId = setInterval(intervalFn, t);

    // Return the cancel function
    return () => {
        clearInterval(intervalId); 
    };
}

// Example usage
const fn1 = (x) => x * 2;
const cancelFn1 = cancellable(fn1, [4], 35);
setTimeout(cancelFn1, 190); 

const fn2 = (x1, x2) => x1 * x2;
const cancelFn2 = cancellable(fn2, [2, 5], 30);
setTimeout(cancelFn2, 165); 

const fn3 = (x1, x2, x3) => x1 + x2 + x3;
const cancelFn3 = cancellable(fn3, [5, 1, 3], 50);
setTimeout(cancelFn3, 180); 
```
['JavaScript']