Two solutions | Using setInterval or recursively call setTimeout
ApproachUsing setInterval is the most direct solution.But calling setTimeout recursively is better if the logic may take longer to execute than the interval tim
# Approach
<!-- Describe your approach to solving the problem. -->
Using setInterval is the most direct solution.
```javascript []
var cancellable = function(fn, args, t) {
    fn(...args);
    const intervalId = setInterval(() => fn(...args), t);
    return () => clearInterval(intervalId);
};
```
But calling setTimeout recursively is better if the logic may take longer to execute than the interval time.
Because it is guarantee that the previous interval has completed before recursing.
You can read more about it in MDN Web Docs.
https://developer.mozilla.org/en-US/docs/Web/API/Window/setInterval#ensure_that_execution_duration_is_shorter_than_interval_frequency

# Code
```javascript []
var cancellable = function(fn, args, t) {
    fn(...args)
    let timeoutId;
    (function loop() {
        timeoutId = setTimeout(() => {
            fn(...args);
            loop();
        }, t)
    })();
    return () => clearTimeout(timeoutId);
};
```
['JavaScript']