{ "data": { "question": { "questionId": "2821", "questionFrontendId": "2715", "boundTopicId": null, "title": "Execute Cancellable Function With Delay", "titleSlug": "execute-cancellable-function-with-delay", "content": "

Given a function fn, an array or arguments args, and a timeout t in milliseconds, return a cancel function cancelFn.

\n\n

After a delay of tfn should be called with args passed as parameters unless cancelFn was called first. In that case, fn should never be called.

\n\n

 

\n

Example 1:

\n\n
\nInput: fn = (x) => x * 5, args = [2], t = 20, cancelTime = 50\nOutput: [{"time": 20, "returned": 10}]\nExplanation: \nconst cancel = cancellable(fn, [2], 20); // fn(2) called at t=20ms\nsetTimeout(cancel, 50);\n\nthe cancelTime (50ms) is after the delay time (20ms), so fn(2) should be called at t=20ms. The value returned from fn is 10.\n
\n\n

Example 2:

\n\n
\nInput: fn = (x) => x**2, args = [2], t = 100, cancelTime = 50\nOutput: []\nExplanation: fn(2) was never called because cancelTime (50ms) is before the delay time (100ms).\n
\n\n

Example 3:

\n\n
\nInput: fn = (x1, x2) => x1 * x2, args = [2,4], t = 30, cancelTime = 100\nOutput: [{"time": 30, "returned": 8}]\nExplanation: fn(2, 4) was called at t=30ms because cancelTime > t.\n
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Easy", "likes": 27, "dislikes": 6, "isLiked": null, "similarQuestions": "[]", "exampleTestcases": "(x) => x * 5\n[2]\n20\n50\n(x) => x**2\n[2]\n100\n50\n(x1, x2) => x1 * x2\n[2,4]\n20\n100", "categoryTitle": "JavaScript", "contributors": [], "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {Function} fn\n * @param {Array} args\n * @param {number} t\n * @return {Function}\n */\nvar cancellable = function(fn, args, t) {\n \n};\n\n/**\n * const result = []\n *\n * const fn = (x) => x * 5\n * const args = [2], t = 20, cancelT = 50\n *\n * const log = (...argsArr) => {\n * result.push(fn(...argsArr))\n * }\n * \n * const cancel = cancellable(fn, args, t);\n * \n * setTimeout(() => {\n * cancel()\n * console.log(result) // [{\"time\":20,\"returned\":10}]\n * }, cancelT)\n */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "function cancellable(fn: Function, args: any[], t: number): Function {\n\n};\n\n/**\n * const result = []\n *\n * const fn = (x) => x * 5\n * const args = [2], t = 20, cancelT = 50\n *\n * const log = (...argsArr) => {\n * result.push(fn(...argsArr))\n * }\n * \n * const cancel = cancellable(fn, args, t);\n * \n * setTimeout(() => {\n * cancel()\n * console.log(result) // [{\"time\":20,\"returned\":10}]\n * }, cancelT)\n */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"1.2K\", \"totalSubmission\": \"1.3K\", \"totalAcceptedRaw\": 1232, \"totalSubmissionRaw\": 1348, \"acRate\": \"91.4%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "(x) => x * 5\n[2]\n20\n50", "metaData": "{\n \"name\": \"cancellable\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"args\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"t\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"cancelT\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.

\"], \"typescript\": [\"Typescript\", \"

TypeScript 4.5.4, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2020 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }