Cancellable Function
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nExplanation\n1. Parameters:\n\nfn:- The function to be executed.\n\nargs:- An array of arguments to be passed to the function fn.\n\nt:- The interval time in milliseconds.\n\n2.  Function Logic:\n\nfn(...args):-  Initially calls the function fn with the provided arguments args.\n\nsetInterval(() => fn(...args), t):- Sets up a timer that calls fn with args every t milliseconds.\n\nclearInterval(timer):- Stops the interval timer.\n\n3.  Return Value:\n\nThe function returns cancelFn, which can be called to stop the repeated execution of fn.\n\n# Complexity\n- Time complexity:- o(1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:- o(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```javascript []\n/**\n * @param {Function} fn\n * @param {Array} args\n * @param {number} t\n * @return {Function}\n */\nvar cancellable = function(fn, args, t) {\n     fn(...args);\n    let timer = setInterval(()=> fn(...args),t);\n\n    let cancelFn = () => clearInterval(timer)\n    return cancelFn;\n};\n\n/**\n *  const result = [];\n *\n *  const fn = (x) => x * 2;\n *  const args = [4], t = 35, cancelTimeMs = 190;\n *\n *  const start = performance.now();\n *\n *  const log = (...argsArr) => {\n *      const diff = Math.floor(performance.now() - start);\n *      result.push({"time": diff, "returned": fn(...argsArr)});\n *  }\n *       \n *  const cancel = cancellable(log, args, t);\n *\n *  setTimeout(cancel, cancelTimeMs);\n *   \n *  setTimeout(() => {\n *      console.log(result); // [\n *                           //     {"time":0,"returned":8},\n *                           //     {"time":35,"returned":8},\n *                           //     {"time":70,"returned":8},\n *                           //     {"time":105,"returned":8},\n *                           //     {"time":140,"returned":8},\n *                           //     {"time":175,"returned":8}\n *                           // ]\n *  }, cancelTimeMs + t + 15)    \n */\n```
['JavaScript']