Interval in JavaScript and TypeScript
Complexity
Time complexity: O(1)
Space complexity: O(1)
Code
# Complexity
- Time complexity: O(1)
- Space complexity: O(1)

# Code
```javascript []
/**
 * @param {Function} fn
 * @param {Array} args
 * @param {number} t
 * @return {Function}
 */
var cancellable = function(fn, args, t) {
    fn(...args);

    const intervalID = setInterval(() => fn(...args), t);
    const clearFn = () => clearInterval(intervalID);

    return clearFn;
};
```

```typescript []
type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
type Fn = (...args: JSONValue[]) => void

function cancellable(fn: Fn, args: JSONValue[], t: number): Function {
    fn(...args);

    const intervalID: ReturnType<typeof setInterval> = setInterval(() => fn(...args), t);
    const clearFn: Function = () => clearInterval(intervalID);

    return clearFn;
};
```
['TypeScript', 'JavaScript']