"content":"<p>Given a function <code>fn</code>, an array of arguments <code>args</code>, and a timeout <code>t</code> in milliseconds, return a cancel function <code>cancelFn</code>.</p>\n\n<p>After a delay of <code>cancelTimeMs</code>, the returned cancel function <code>cancelFn</code> will be invoked.</p>\n\n<pre>\nsetTimeout(cancelFn, cancelTimeMs)\n</pre>\n\n<p>Initially, the execution of the function <code>fn</code> should be delayed by <code>t</code> milliseconds.</p>\n\n<p>If, before the delay of <code>t</code> milliseconds, the function <code>cancelFn</code> is invoked, it should cancel the delayed execution of <code>fn</code>. Otherwise, if <code>cancelFn</code> is not invoked within the specified delay <code>t</code>, <code>fn</code> should be executed with the provided <code>args</code> as arguments.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x) => x * 5, args = [2], t = 20\n<strong>Output:</strong> [{"time": 20, "returned": 10}]\n<strong>Explanation:</strong> \nconst cancelTimeMs = 50;\nconst result = [];\n\nconst fn = (x) => x * 5;\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, [2], 20);\n\nconst maxT = Math.max(t, 50);\n \nsetTimeout(cancel, cancelTimeMs);\n\nsetTimeout(() => {\n console.log(result); // [{"time":20,"returned":10}]\n}, maxT + 15);\n\nThe cancellation was scheduled to occur after a delay of cancelTimeMs (50ms), which happened after the execution of fn(2) at 20ms.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x) => x**2, args = [2], t = 100\n<strong>Output:</strong> []\n<strong>Explanation:</strong> \nconst cancelTimeMs = 50;\nThe cancellation was scheduled to occur after a delay of cancelTimeMs (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x1, x2) => x1 * x2, args = [2,4], t = 30\n<strong>Output:</strong> [{"time": 30, "returned": 8}]\n<strong>Explanation: \n</strong>const cancelTimeMs = 100;\nThe cancellation was scheduled to occur after a delay of cancelTimeMs (100ms), which happened after the execution of fn(2,4) at 30ms.\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\">20 <= t <= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">10 <= cancelTimeMs <= 1000</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>\"]}",