{ "data": { "question": { "questionId": "2862", "questionFrontendId": "2725", "boundTopicId": null, "title": "Interval Cancellation", "titleSlug": "interval-cancellation", "content": "

Given a function fn, an array of arguments args, and an interval time t, return a cancel function cancelFn.

\n\n

The function fn should be called with args immediately and then called again every t milliseconds until cancelFn is called at cancelTimeMs ms.

\n\n

 

\n

Example 1:

\n\n
\nInput: fn = (x) => x * 2, args = [4], t = 35\nOutput: \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]\nExplanation: \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
\n\n

Example 2:

\n\n
\nInput: fn = (x1, x2) => (x1 * x2), args = [2, 5], t = 30\nOutput: \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]\nExplanation: \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
\n\n

Example 3:

\n\n
\nInput: fn = (x1, x2, x3) => (x1 + x2 + x3), args = [5, 1, 3], t = 50\nOutput: \n[\n   {"time": 0, "returned": 9},\n   {"time": 50, "returned": 9},\n   {"time": 100, "returned": 9},\n   {"time": 150, "returned": 9}\n]\nExplanation: \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
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Easy", "likes": 109, "dislikes": 67, "isLiked": null, "similarQuestions": "[]", "exampleTestcases": "(x) => x * 2\n[4]\n35\n190\n(x1, x2) => (x1 * x2)\n[2,5]\n30\n165\n(x1, x2, x3) => (x1 + x2 + x3)\n[5,1,3]\n50\n180", "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 * 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 */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };\ntype Fn = (...args: JSONValue[]) => void\n\nfunction cancellable(fn: Fn, args: JSONValue[], t: number): Function {\n\t\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 */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"16.1K\", \"totalSubmission\": \"22.1K\", \"totalAcceptedRaw\": 16058, \"totalSubmissionRaw\": 22053, \"acRate\": \"72.8%\"}", "hints": [], "solution": { "id": "1991", "canSeeDetail": true, "paidOnly": false, "hasVideoSolution": false, "paidOnlyVideo": true, "__typename": "ArticleNode" }, "status": null, "sampleTestCase": "(x) => x * 2\n[4]\n35\n190", "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\": \"cancelTimeMs\"\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 5.1.6, Node.js 16.13.2.

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

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

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

lodash.js library is included by default.

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