Interval Cancellation
Intuition\nTo execute a function repeatedly at specified intervals and allow for cancellation, I will use setInterval for repeated calls and setTimeout for mana
# Intuition\nTo execute a function repeatedly at specified intervals and allow for cancellation, I will use setInterval for repeated calls and setTimeout for managing the cancellation.\n# Approach\nI will store the results of each function call in an array, execute the function immediately, and set an interval for subsequent calls, while providing a cancel function that stops further executions and returns the collected results.\n\n# Complexity\n- Time complexity:O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\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    let results = [];\n    let cancelled= true;\n    //step1 : called immediately\n    const firstresult=fn(...args);\n    results.push({time :0, returned :firstresult});\n    //step 2 : after t milliseconds\n    const tid= setInterval(()=>{\n        if(cancelled){\n            const time= results.length*t;\n            const result=fn(...args);\n            results.push({time, returned: result});\n        }\n    }, t);\n    //step 3 : cancelFn\n    return function cancelFn(){\n        cancelled=false;//mark as cancelled\n        clearInterval(tid);\n        return results;\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']