2725. 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 []
/**
 * @param {Function} fn
 * @param {Array} args
 * @param {number} t
 * @return {Function}
 */
var cancellable = function(fn, args, t) {
    let time = 0;
    let result = [];

    // Function that records the result and calls fn
    const logFn = () => {
        result.push({ "time": time, "returned": fn(...args) });
        time += t;
    };

    // Call the function immediately
    logFn();

    // Set an interval for repeated calls every `t` milliseconds
    const intervalId = setInterval(logFn, t);

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

```
['JavaScript']