Timeout 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) {
    const timeoutID = setTimeout(() => {
        fn.apply(null, args);
    }, t);

    const cancelFn = () => clearTimeout(timeoutID);
    return cancelFn;
};
```

```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 {
    const timeoutID: ReturnType<typeof setTimeout> = setTimeout(() => {
        fn.apply(null, args);
    }, t);

    const cancelFn: Function = () => clearTimeout(timeoutID);
    return cancelFn;
};
```
['TypeScript', 'JavaScript']