Simple Solution with explanation of the program and approach to solve the problem step by step
IntuitionUnderstand the given condition and think the logic to the given problem and the return value.ApproachHear we can create a function that which can clear
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Understand the given condition and think the logic to the given problem and the return value.
# Approach
<!-- Describe your approach to solving the problem. -->
Hear we can create a function that which can clear the timer.
# Breakedown of problem
- We have a timer `t`, arguments `args` and a function `fn`.
- we have to call a `setTimeout()` which execute a `fn`.
- We have a another function `cancelFn` which cancel the timer.
- If the function `cancelFn` is invoked before the `t` milliseconds it should cancel the `fn` execution. If it is not invoked `fn` should be executed with arguments `args`.
- We use `clearTimer` inside the `cancelFn` to clear the timer.
# Breakdown of program
- Here we have a function called `cancellable` with arguments as `fn` function,`args` arguments for fn function,`t` timer which return a function called `cancelFn`.
- `let cancelFn = function(){ clearTimeout(timer) }` it is cancel function which clear the timer `t` using the method `cancleTimer` by passing.
- `timer` is variable which store the return value of the `setTimeout(()=>{ fn(...args) },t)`.
- `setTimeout(()=>{ fn(...args) },t)` which wit for the `t` milliseconds and execute the `fn` function.
- `return cancelFn` which call the `cancelFn` and execute it.
# Complexity
- Time complexity:O(1)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
because it performs a constant amount of work regardless of the size of the input.
- Space complexity:O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
ecause the function uses a fixed amount of space for the timer and the `cancelFn`.
# Code
```javascript []
var cancellable = function(fn, args, t) {
    let cancelFn = function(){
        clearTimeout(timer)
    }
    let timer = setTimeout(()=>{
        fn(...args)
    },t)
    return cancelFn
};

```
['JavaScript']