"content":"<p>Given a function <code>fn</code>, an array of arguments <code>args</code>, and an interval time <code>t</code>, return a cancel function <code>cancelFn</code>.</p>\n\n<p>The function <code>fn</code> should be called with <code>args</code> immediately and then called again every <code>t</code> milliseconds until <code>cancelFn</code> is called at <code>cancelTimeMs</code> ms.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x) => x * 2, args = [4], t = 35\n<strong>Output:</strong> \n[\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<strong>Explanation:</strong> \nconst result = [];\nconst fn = (x) => x * 2;\nconst cancelTimeMs = 190;\n\nconst start = performance.now();\n\nconst log = (...argsArr) => {\n const diff = Math.floor(performance.now() - start);\n result.push({"time": diff, "returned": fn(...argsArr)});\n}\n\nconst cancel = cancellable(log, [4], 35);\nsetTimeout(cancel, cancelTimeMs);\n\nsetTimeout(() => {\n console.log(result); // Output\n }, cancelTimeMs + 50) \n\nEvery 35ms, fn(4) is called. Until t=190ms, then it is cancelled.\n1st fn call is at 0ms. fn(4) returns 8.\n2nd fn call is at 35ms. fn(4) returns 8.\n3rd fn call is at 70ms. fn(4) returns 8.\n4th fn call is at 105ms. fn(4) returns 8.\n5th fn call is at 140ms. fn(4) returns 8.\n6th fn call is at 175ms. fn(4) returns 8.\nCancelled at 190ms\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x1, x2) => (x1 * x2), args = [2, 5], t = 30\n<strong>Output:</strong> \n[\n {"time": 0, "returned": 10},\n {"time": 30, "returned": 10},\n {"time": 60, "returned": 10},\n {"time": 90, "returned": 10},\n {"time": 120, "returned": 10},\n {"time": 150, "returned": 10}\n]\n<strong>Explanation:</strong> \nconst cancelTimeMs = 165;\n\nEvery 30ms, fn(2, 5) is called. Until t=165ms, then it is cancelled.\n1st fn call is at 0ms \n2nd fn call is at 30ms \n3rd fn call is at 60ms \n4th fn call is at 90ms \n5th fn call is at 120ms \n6th fn call is at 150ms\nCancelled at 165ms\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x1, x2, x3) => (x1 + x2 + x3), args = [5, 1, 3], t = 50\n<strong>Output:</strong> \n[\n {"time": 0, "returned": 9},\n {"time": 50, "returned": 9},\n {"time": 100, "returned": 9},\n {"time": 150, "returned": 9}\n]\n<strong>Explanation:</strong> \nconst cancelTimeMs = 180;\n\nEvery 50ms, fn(5, 1, 3) is called. Until t=180ms, then it is cancelled. \n1st fn call is at 0ms\n2nd fn call is at 50ms\n3rd fn call is at 100ms\n4th fn call is at 150ms\nCancelled at 180ms\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>fn</code> is a function</li>\n\t<li><code>args</code> is a valid JSON array</li>\n\t<li><code>1 <= args.length <= 10</code></li>\n\t<li><code><font face=\"monospace\">30 <= t <= 100</font></code></li>\n\t<li><code><font face=\"monospace\">10 <= </font>cancelTimeMs<font face=\"monospace\"> <= 500</font></code></li>\n</ul>\n",
"envInfo":"{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 5.1.6, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2022 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",