|
6 | 6 |
|
7 | 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 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> |
| 9 | +<p>After a delay of <code>cancelT</code>, the returned cancel function <code>cancelFn</code> will be invoked.</p> |
| 10 | + |
| 11 | +<pre> |
| 12 | +setTimeout(cancelFn, cancelT) |
| 13 | +</pre> |
| 14 | + |
| 15 | +<p>Initially, the execution of the function <code>fn</code> should be delayed by <code>t</code> milliseconds.</p> |
| 16 | + |
| 17 | +<p>If, before the delay of <code>t</code> milliseconds, the function <code>cancelFn</code> is invoked, it should cancel the delayed execution of <code>fn</code>. Otherwise, if <code>cancelFn</code> is not invoked within the specified delay <code>t</code>, <code>fn</code> should be executed with the provided <code>args</code> as arguments.</p> |
10 | 18 |
|
11 | 19 | <p> </p>
|
12 | 20 | <p><strong class="example">Example 1:</strong></p>
|
13 | 21 |
|
14 | 22 | <pre>
|
15 |
| -<strong>Input:</strong> fn = (x) => x * 5, args = [2], t = 20, cancelT = 50 |
| 23 | +<strong>Input:</strong> fn = (x) => x * 5, args = [2], t = 20 |
16 | 24 | <strong>Output:</strong> [{"time": 20, "returned": 10}]
|
17 | 25 | <strong>Explanation:</strong>
|
18 |
| -const result = [] |
| 26 | +const cancelT = 50; |
| 27 | +const result = []; |
19 | 28 |
|
20 |
| -const fn = (x) => x * 5 |
| 29 | +const fn = (x) => x * 5; |
21 | 30 |
|
22 |
| -const start = performance.now() |
| 31 | +const start = performance.now(); |
23 | 32 |
|
24 | 33 | const log = (...argsArr) => {
|
25 | 34 | const diff = Math.floor(performance.now() - start);
|
26 |
| - result.push({"time": diff, "returned": fn(...argsArr)}) |
| 35 | + result.push({"time": diff, "returned": fn(...argsArr)}); |
27 | 36 | }
|
28 | 37 |
|
29 | 38 | const cancel = cancellable(log, [2], 20);
|
30 | 39 |
|
31 |
| -const maxT = Math.max(t, 50) |
| 40 | +const maxT = Math.max(t, 50); |
32 | 41 |
|
33 |
| -setTimeout(cancel, cancelT) |
| 42 | +setTimeout(cancel, cancelT); |
34 | 43 |
|
35 | 44 | setTimeout(() => {
|
36 |
| - console.log(result) // [{"time":20,"returned":10}] |
37 |
| -}, 65) |
| 45 | + console.log(result); // [{"time":20,"returned":10}] |
| 46 | +}, 65); |
38 | 47 |
|
39 | 48 | The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened after the execution of fn(2) at 20ms.
|
40 | 49 | </pre>
|
41 | 50 |
|
42 | 51 | <p><strong class="example">Example 2:</strong></p>
|
43 | 52 |
|
44 | 53 | <pre>
|
45 |
| -<strong>Input:</strong> fn = (x) => x**2, args = [2], t = 100, cancelT = 50 |
| 54 | +<strong>Input:</strong> fn = (x) => x**2, args = [2], t = 100 |
46 | 55 | <strong>Output:</strong> []
|
47 |
| -<strong>Explanation:</strong> 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. |
| 56 | +<strong>Explanation:</strong> |
| 57 | +const cancelT = 50; |
| 58 | +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. |
48 | 59 | </pre>
|
49 | 60 |
|
50 | 61 | <p><strong class="example">Example 3:</strong></p>
|
51 | 62 |
|
52 | 63 | <pre>
|
53 |
| -<strong>Input:</strong> fn = (x1, x2) => x1 * x2, args = [2,4], t = 30, cancelT = 100 |
| 64 | +<strong>Input:</strong> fn = (x1, x2) => x1 * x2, args = [2,4], t = 30 |
54 | 65 | <strong>Output:</strong> [{"time": 30, "returned": 8}]
|
55 |
| -<strong>Explanation: </strong>The cancellation was scheduled to occur after a delay of cancelT (100ms), which happened after the execution of fn(2,4) at 30ms. |
| 66 | +<strong>Explanation: |
| 67 | +</strong>const cancelT = 100; |
| 68 | +The cancellation was scheduled to occur after a delay of cancelT (100ms), which happened after the execution of fn(2,4) at 30ms. |
56 | 69 | </pre>
|
57 | 70 |
|
58 | 71 | <p> </p>
|
|
0 commit comments