Js Easy
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to repeatedly invoke a function (fn) with specified arguments (args) at a g
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to repeatedly invoke a function (fn) with specified arguments (args) at a given time interval (t). The process should allow cancellation of the repeated invocation through a returned function.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nInitial Call: Immediately call the function fn with the arguments args.\nRepeated Execution: Use setInterval to execute fn(...args) at the interval t.\nCancellation Mechanism: Return a cancelFn that clears the interval using clearInterval(timer), stopping further invocations.\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(1)\n- Space complexity:\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(()=>{\n        fn(...args)\n    },t)\n    let canlcFn = ()=>{\n        clearInterval(timer)\n    }\n    return canlcFn\n\n    \n};\n\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']