|
1 |
| -# [2715. Execute Cancellable Function With Delay](https://leetcode.com/problems/execute-cancellable-function-with-delay) |
| 1 | +# [2715. Timeout Cancellation](https://leetcode.com/problems/timeout-cancellation) |
2 | 2 |
|
3 |
| -[中文文档](/solution/2700-2799/2715.Execute%20Cancellable%20Function%20With%20Delay/README.md) |
| 3 | +[中文文档](/solution/2700-2799/2715.Timeout%20Cancellation/README.md) |
4 | 4 |
|
5 | 5 | ## Description
|
6 | 6 |
|
7 |
| -<p>Given a function <code>fn</code>, an array or arguments <code>args</code>, and a timeout <code>t</code> in milliseconds, return a cancel function <code>cancelFn</code>.</p> |
| 7 | +<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> |
8 | 8 |
|
9 |
| -<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 called first. In that case, <code>fn</code> should never be called.</p> |
| 9 | +<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> |
10 | 10 |
|
11 | 11 | <p> </p>
|
12 | 12 | <p><strong class="example">Example 1:</strong></p>
|
13 | 13 |
|
14 | 14 | <pre>
|
15 |
| -<strong>Input:</strong> fn = (x) => x * 5, args = [2], t = 20 |
| 15 | +<strong>Input:</strong> fn = (x) => x * 5, args = [2], t = 20, cancelT = 50 |
16 | 16 | <strong>Output:</strong> [{"time": 20, "returned": 10}]
|
17 | 17 | <strong>Explanation:</strong>
|
18 |
| -const cancelTime = 50 |
19 | 18 | const cancel = cancellable((x) => x * 5, [2], 20); // fn(2) called at t=20ms
|
20 |
| -setTimeout(cancel, cancelTime); |
| 19 | +setTimeout(cancel, 50); |
21 | 20 |
|
22 |
| -The cancellation was scheduled to occur after a delay of cancelTime (50ms), which happened after the execution of fn(2) at 20ms. |
| 21 | +The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened after the execution of fn(2) at 20ms. |
23 | 22 | </pre>
|
24 | 23 |
|
25 | 24 | <p><strong class="example">Example 2:</strong></p>
|
26 | 25 |
|
27 | 26 | <pre>
|
28 |
| -<strong>Input:</strong> fn = (x) => x**2, args = [2], t = 100 |
| 27 | +<strong>Input:</strong> fn = (x) => x**2, args = [2], t = 100, cancelT = 50 |
29 | 28 | <strong>Output:</strong> []
|
30 | 29 | <strong>Explanation:</strong>
|
31 |
| -const cancelTime = 50 |
32 | 30 | const cancel = cancellable((x) => x**2, [2], 100); // fn(2) not called
|
33 |
| -setTimeout(cancel, cancelTime); |
| 31 | +setTimeout(cancel, 50); |
34 | 32 |
|
35 |
| -The cancellation was scheduled to occur after a delay of cancelTime (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called. |
| 33 | +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. |
36 | 34 | </pre>
|
37 | 35 |
|
38 | 36 | <p><strong class="example">Example 3:</strong></p>
|
39 | 37 |
|
40 | 38 | <pre>
|
41 |
| -<strong>Input:</strong> fn = (x1, x2) => x1 * x2, args = [2,4], t = 30 |
| 39 | +<strong>Input:</strong> fn = (x1, x2) => x1 * x2, args = [2,4], t = 30, cancelT = 100 |
42 | 40 | <strong>Output:</strong> [{"time": 30, "returned": 8}]
|
43 | 41 | <strong>Explanation:</strong>
|
44 |
| -const cancelTime = 100 |
45 | 42 | const cancel = cancellable((x1, x2) => x1 * x2, [2,4], 30); // fn(2,4) called at t=30ms
|
46 |
| -setTimeout(cancel, cancelTime); |
| 43 | +setTimeout(cancel, 100); |
47 | 44 |
|
48 |
| -The cancellation was scheduled to occur after a delay of cancelTime (100ms), which happened after the execution of fn(2,4) at 30ms. |
| 45 | +The cancellation was scheduled to occur after a delay of cancelT (100ms), which happened after the execution of fn(2,4) at 30ms. |
49 | 46 | </pre>
|
50 | 47 |
|
51 | 48 | <p> </p>
|
|
0 commit comments