|
6 | 6 |
|
7 | 7 | <!-- 这里写题目描述 -->
|
8 | 8 |
|
9 |
| -<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> |
| 9 | +<p>现给定一个函数 <code>fn</code> ,一个参数数组 <code>args</code> 和一个以毫秒为单位的超时时间 <code>t</code> ,返回一个取消函数 <code>cancelFn</code> 。</p> |
10 | 10 |
|
11 |
| -<p>After a delay of <code>t</code>, <code>fn</code> should be called with <code>args</code> passed as parameters <strong>unless</strong> <code>cancelFn</code> was invoked before the delay of <code>t</code> milliseconds elapses, specifically at <code>cancelT</code> ms. In that case, <code>fn</code> should never be called.</p> |
| 11 | +<p>在经过 <code>t</code> 毫秒的延迟后,应该调用 <code>fn</code> 函数,并将 <code>args</code> 作为参数传递。<strong>除非</strong> 在 <code>t</code> 毫秒的延迟过程中,在 <code>cancelT</code> 毫秒时调用了 <code>cancelFn</code>。并且在这种情况下,<code>fn</code> 函数不应该被调用。</p> |
12 | 12 |
|
13 |
| -<p> </p> |
14 |
| -<p><strong class="example">Example 1:</strong></p> |
| 13 | +<p><strong class="example">示例 1:</strong></p> |
15 | 14 |
|
16 | 15 | <pre>
|
17 |
| -<strong>Input:</strong> fn = (x) => x * 5, args = [2], t = 20, cancelT = 50 |
18 |
| -<strong>Output:</strong> [{"time": 20, "returned": 10}] |
19 |
| -<strong>Explanation:</strong> |
20 |
| -const cancel = cancellable((x) => x * 5, [2], 20); // fn(2) called at t=20ms |
| 16 | +<b>输入:</b>fn = (x) => x * 5, args = [2], t = 20, cancelT = 50 |
| 17 | +<b>输出:</b>[{"time": 20, "returned": 10}] |
| 18 | +<b>解释:</b> |
| 19 | +const cancel = cancellable((x) => x * 5, [2], 20); // fn(2) 在 t=20ms 时被调用 |
21 | 20 | setTimeout(cancel, 50);
|
22 | 21 |
|
23 |
| -The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened after the execution of fn(2) at 20ms. |
24 |
| -</pre> |
| 22 | +取消操作被安排在延迟了 cancelT(50毫秒)后进行,这发生在 fn(2) 在20毫秒时执行之后。</pre> |
25 | 23 |
|
26 |
| -<p><strong class="example">Example 2:</strong></p> |
| 24 | +<p><strong class="example">示例 2:</strong></p> |
27 | 25 |
|
28 | 26 | <pre>
|
29 |
| -<strong>Input:</strong> fn = (x) => x**2, args = [2], t = 100, cancelT = 50 |
30 |
| -<strong>Output:</strong> [] |
31 |
| -<strong>Explanation:</strong> |
32 |
| -const cancel = cancellable((x) => x**2, [2], 100); // fn(2) not called |
| 27 | +<b>输入:</b>fn = (x) => x**2, args = [2], t = 100, cancelT = 50 |
| 28 | +<b>输出:</b>[] |
| 29 | +<b>解释:</b> |
| 30 | +const cancel = cancellable((x) => x**2, [2], 100); // fn(2) 没被调用 |
33 | 31 | setTimeout(cancel, 50);
|
34 | 32 |
|
35 |
| -The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called. |
| 33 | +取消操作被安排在延迟了 cancelT(50毫秒)后进行,这发生在 fn(2) 在100毫秒时执行之前,导致 fn(2) 从未被调用。 |
36 | 34 | </pre>
|
37 | 35 |
|
38 |
| -<p><strong class="example">Example 3:</strong></p> |
| 36 | +<p><strong class="example">示例 3:</strong></p> |
39 | 37 |
|
40 | 38 | <pre>
|
41 |
| -<strong>Input:</strong> fn = (x1, x2) => x1 * x2, args = [2,4], t = 30, cancelT = 100 |
42 |
| -<strong>Output:</strong> [{"time": 30, "returned": 8}] |
43 |
| -<strong>Explanation:</strong> |
44 |
| -const cancel = cancellable((x1, x2) => x1 * x2, [2,4], 30); // fn(2,4) called at t=30ms |
| 39 | +<b>输入:</b>fn = (x1, x2) => x1 * x2, args = [2,4], t = 30, cancelTime = 100 |
| 40 | +<b>输出:</b>[{"time": 30, "returned": 8}] |
| 41 | +<b>解释:</b> |
| 42 | +const cancel = cancellable((x1, x2) => x1 * x2, [2,4], 30); // fn(2,4) 在 t=30ms 时被调用 |
45 | 43 | setTimeout(cancel, 100);
|
46 | 44 |
|
47 |
| -The cancellation was scheduled to occur after a delay of cancelT (100ms), which happened after the execution of fn(2,4) at 30ms. |
| 45 | +取消操作被安排在延迟了 cancelT(100毫秒)后进行,这发生在 fn(2,4) 在30毫秒时执行之后。 |
48 | 46 | </pre>
|
49 | 47 |
|
50 | 48 | <p> </p>
|
51 |
| -<p><strong>Constraints:</strong></p> |
| 49 | + |
| 50 | +<p><strong>提示:</strong></p> |
52 | 51 |
|
53 | 52 | <ul>
|
54 |
| - <li><code>fn is a function</code></li> |
55 |
| - <li><code>args is a valid JSON array</code></li> |
| 53 | + <li><code>fn 是一个函数</code></li> |
| 54 | + <li><code>args 是一个有效的 JSON 数组</code></li> |
56 | 55 | <li><code>1 <= args.length <= 10</code></li>
|
57 | 56 | <li><code><font face="monospace">20 <= t <= 1000</font></code></li>
|
58 | 57 | <li><code><font face="monospace">10 <= cancelT <= 1000</font></code></li>
|
|
0 commit comments