{ "data": { "question": { "questionId": "2862", "questionFrontendId": "2725", "categoryTitle": "JavaScript", "boundTopicId": 2300584, "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. The function fn should be called with args immediately and then called again every t milliseconds until cancelFn is called.\n

 

\n

Example 1:

\n\n
\nInput: fn = (x) => x * 2, args = [4], t = 20, cancelT = 110\nOutput: \n[\n   {"time": 0, "returned": 8},\n   {"time": 20, "returned": 8},\n   {"time": 40, "returned": 8},\n   {"time": 60, "returned": 8},\n   {"time": 80, "returned": 8},\n   {"time": 100, "returned": 8}\n]\nExplanation: Every 20ms, fn(4) is called. Until t=110ms, then it is cancelled.\n\nconst cancel = cancellable(x => x * 2, [4], 20);\nsetTimeout(cancel, 110);\n\n1st fn call is at 0ms. fn(4) returns 8.\n2nd fn call is at 20ms. fn(4) returns 8.\n3rd fn call is at 40ms. fn(4) returns 8.\n4th fn call is at 60ms. fn(4) returns 8.\n5th fn call is at 80ms. fn(4) returns 8.\n6th fn call is at 100ms. fn(4) returns 8.\n
\n\n

Example 2:

\n\n
\nInput: fn = (x1, x2) => (x1 * x2), args = [2, 5], t = 25, cancelT = 140\nOutput: \n[\n   {"time": 0, "returned": 10},\n   {"time": 25, "returned": 10},\n   {"time": 50, "returned": 10},\n   {"time": 75, "returned": 10},\n   {"time": 100, "returned": 10},\n   {"time": 125, "returned": 10}\n]\nExplanation: Every 25ms, fn(2, 5) is called. Until t=140ms, then it is cancelled.\n1st fn call is at 0ms \n2nd fn call is at 25ms \n3rd fn call is at 50ms \n4th fn call is at 75ms \n5th fn call is at 100ms \n6th fn call is at 125ms\nCancelled at 140ms\n
\n\n

Example 3:

\n\n
\nInput: fn = (x1, x2, x3) => (x1 + x2 + x3), args = [5, 1, 3], t = 50, cancelT = 180\nOutput: \n[\n   {"time": 0, "returned": 9},\n   {"time": 50, "returned": 9},\n   {"time": 100, "returned": 9},\n   {"time": 150, "returned": 9}\n]\nExplanation: Every 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": "间隔取消", "translatedContent": "现给定一个函数 fn,一个参数数组 args 和一个时间间隔 t,返回一个取消函数 cancelFn。函数 fn 应该立即使用 args 调用,并且在每个 t 毫秒内再次调用,直到调用 cancelFn。\n

 

\n\n

示例 1:

\n\n
\n输入:fn = (x) => x * 2, args = [4], t = 20, cancelT = 110\n输出:\n[\n   {\"time\": 0, \"returned\": 8},\n   {\"time\": 20, \"returned\": 8},\n   {\"time\": 40, \"returned\": 8},\n   {\"time\": 60, \"returned\": 8},\n   {\"time\": 80, \"returned\": 8},\n   {\"time\": 100, \"returned\": 8}\n]\n解释: 每隔 20ms,调用 fn(4)。直到 t=110ms,然后取消。\nconst cancel = cancellable(x => x * 2, [4], 20);\nsetTimeout(cancel, 110);\n第一次调用 fn 是在 0ms。fn(4) 返回 8。\n第二次调用 fn 是在 20ms。fn(4) 返回 8。\n第三次调用 fn 是在 40ms。fn(4) 返回 8。\n第四次调用 fn 是在 60ms。fn(4) 返回 8。\n第五次调用 fn 是在 80ms。fn(4) 返回 8。\n第六次调用 fn 是在 100ms。fn(4) 返回 8。
\n\n

示例 2:

\n\n
\n输入:fn = (x1, x2) => (x1 * x2), args = [2, 5], t = 25, cancelT = 140\n输出: \n[\n   {\"time\": 0, \"returned\": 10},\n   {\"time\": 25, \"returned\": 10},\n   {\"time\": 50, \"returned\": 10},\n   {\"time\": 75, \"returned\": 10},\n   {\"time\": 100, \"returned\": 10},\n   {\"time\": 125, \"returned\": 10}\n]\n解释:每隔 25ms,调用 fn(2, 5)。直到 t=140ms,然后取消。\n第一次调用 fn 是在 0ms\n第二次调用 fn 是在 25ms\n第三次调用 fn 是在 50ms\n第四次调用 fn 是在 75ms\n第五次调用 fn 是在 100ms\n第六次调用 fn 是在 125ms\n在 140ms 取消\n
\n\n

示例 3:

\n\n
\n输入:fn = (x1, x2, x3) => (x1 + x2 + x3), args = [5, 1, 3], t = 50, cancelT = 180\n输出:\n[\n   {\"time\": 0, \"returned\": 9},\n   {\"time\": 50, \"returned\": 9},\n   {\"time\": 100, \"returned\": 9},\n   {\"time\": 150, \"returned\": 9}\n]\n解释:每隔 50ms,调用 fn(5, 1, 3)。直到 t=180ms,然后取消。\n第一次调用 fn 是在 0ms\n第二次调用 fn 是在 50ms\n第三次调用 fn 是在 100ms\n第四次调用 fn 是在 150ms\n在 180ms 取消\n
\n\n

 

\n\n

提示:

\n\n\n", "isPaidOnly": false, "difficulty": "Easy", "likes": 0, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}", "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 = 20, cancelT = 110\n *\n * const start = performance.now()\n *\n * const log = (...argsArr) => {\n *\t\tconst val = fn(...argsArr)\n * result.push({\"time\": Math.floor(performance.now() - start), \"returned\": fn(...argsArr)})\n * }\n * \n * const cancel = cancellable(log, args, t);\n * \n * setTimeout(() => {\n * cancel()\n * console.log(result) // [\n * // {\"time\":0,\"returned\":8},\n * // {\"time\":20,\"returned\":8},\n * // {\"time\":40,\"returned\":8}, \n * // {\"time\":60,\"returned\":8},\n * // {\"time\":80,\"returned\":8},\n * // {\"time\":100,\"returned\":8}\n * // ]\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 * 2\n * const args = [4], t = 20, cancelT = 110\n *\n * const start = performance.now()\n *\n * const log = (...argsArr) => {\n *\t\tconst val = fn(...argsArr)\n * result.push({\"time\": Math.floor(performance.now() - start), \"returned\": fn(...argsArr)})\n * }\n * \n * const cancel = cancellable(log, args, t);\n * \n * setTimeout(() => {\n * cancel()\n * console.log(result) // [\n * // {\"time\":0,\"returned\":8},\n * // {\"time\":20,\"returned\":8},\n * // {\"time\":40,\"returned\":8}, \n * // {\"time\":60,\"returned\":8},\n * // {\"time\":80,\"returned\":8},\n * // {\"time\":100,\"returned\":8}\n * // ]\n * }, cancelT)\n */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"139\", \"totalSubmission\": \"176\", \"totalAcceptedRaw\": 139, \"totalSubmissionRaw\": 176, \"acRate\": \"79.0%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "(x) => x * 2\n[4]\n20\n110", "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, "envInfo": "{\"javascript\":[\"JavaScript\",\"

\\u7248\\u672c\\uff1aNode.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n

\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a --harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"

TypeScript 4.5.4<\\/p>\\r\\n\\r\\n

Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "(x) => x * 2\n[4]\n20\n110\n(x1, x2) => (x1 * x2)\n[2,5]\n25\n140\n(x1, x2, x3) => (x1 + x2 + x3)\n[5,1,3]\n50\n160", "__typename": "QuestionNode" } } }