{ "data": { "question": { "questionId": "2821", "questionFrontendId": "2715", "categoryTitle": "JavaScript", "boundTopicId": 2293968, "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
.
After a delay of t
, fn
should be called with args
passed as parameters unless cancelFn
was called first. In that case, fn
should never be called.
\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\nfn is a function
args is a valid JSON array
1 <= args.length <= 10
20 <= t <= 1000
10 <= cancelT <= 1000
现给定一个函数 fn
,一个参数数组 args
和一个以毫秒为单位的超时时间 t
,返回一个取消函数 cancelFn
。
在经过 t
毫秒的延迟后,除非 先调用 cancelFn
,否则 fn
应该以 args
作为参数被调用。并且在这种情况下,fn
不应该被调用。
\n\n
示例 1:
\n\n\n输入:fn = (x) => x * 5, args = [2], t = 20, cancelTime = 50\n输出:[{\"time\": 20, \"returned\": 10}]\n解释:\nconst cancel = cancellable(fn, [2], 20); // // 在 t=20ms 时调用 fn(2)\nsetTimeout(cancel, 50);\n\ncancelTime(50ms)在延迟时间(20ms)之后,所以 fn(2) 应该在 t=20ms 时调用。fn 的返回值是 10。\n\n\n
示例 2:
\n\n\n输入:fn = (x) => x**2, args = [2], t = 100, cancelTime = 50\n输出:[]\n解释:fn(2) 从未被调用,因为 cancelTime(50ms)在延迟时间(100ms)之前。\n\n\n
示例 3:
\n\n\n输入:fn = (x1, x2) => x1 * x2, args = [2,4], t = 30, cancelTime = 100\n输出:[{\"time\": 30, \"returned\": 8}]\n解释:fn(2) 从未被调用,因为 cancelTime(50ms)在延迟时间(100ms)之前。\n\n\n
\n\n
提示:
\n\nfn 是一个函数
args 是一个有效的 JSON 数组
1 <= args.length <= 10
20 <= t <= 1000
10 <= cancelT <= 1000
\\u7248\\u672c\\uff1a \\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a 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 * 5\n[2]\n20\n50\n(x) => x**2\n[2]\n100\n50\n(x1, x2) => x1 * x2\n[2,4]\n20\n100",
"__typename": "QuestionNode"
}
}
}Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n
--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n