mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 23:41:41 +08:00
存量题库数据更新
This commit is contained in:
@@ -1,51 +1,74 @@
|
||||
<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>
|
||||
|
||||
<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>
|
||||
<p>After a delay of <code>cancelTimeMs</code>, the returned cancel function <code>cancelFn</code> will be invoked.</p>
|
||||
|
||||
<pre>
|
||||
setTimeout(cancelFn, cancelTimeMs)
|
||||
</pre>
|
||||
|
||||
<p>Initially, the execution of the function <code>fn</code> should be delayed by <code>t</code> milliseconds.</p>
|
||||
|
||||
<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>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> fn = (x) => x * 5, args = [2], t = 20, cancelT = 50
|
||||
<strong>Input:</strong> fn = (x) => x * 5, args = [2], t = 20
|
||||
<strong>Output:</strong> [{"time": 20, "returned": 10}]
|
||||
<strong>Explanation:</strong>
|
||||
const cancel = cancellable((x) => x * 5, [2], 20); // fn(2) called at t=20ms
|
||||
setTimeout(cancel, 50);
|
||||
const cancelTimeMs = 50;
|
||||
const result = [];
|
||||
|
||||
The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened after the execution of fn(2) at 20ms.
|
||||
const fn = (x) => x * 5;
|
||||
|
||||
const start = performance.now();
|
||||
|
||||
const log = (...argsArr) => {
|
||||
const diff = Math.floor(performance.now() - start);
|
||||
result.push({"time": diff, "returned": fn(...argsArr)});
|
||||
}
|
||||
|
||||
const cancel = cancellable(log, [2], 20);
|
||||
|
||||
const maxT = Math.max(t, 50);
|
||||
|
||||
setTimeout(cancel, cancelTimeMs);
|
||||
|
||||
setTimeout(() => {
|
||||
console.log(result); // [{"time":20,"returned":10}]
|
||||
}, maxT + 15);
|
||||
|
||||
The cancellation was scheduled to occur after a delay of cancelTimeMs (50ms), which happened after the execution of fn(2) at 20ms.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> fn = (x) => x**2, args = [2], t = 100, cancelT = 50
|
||||
<strong>Input:</strong> fn = (x) => x**2, args = [2], t = 100
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong>
|
||||
const cancel = cancellable((x) => x**2, [2], 100); // fn(2) not called
|
||||
setTimeout(cancel, 50);
|
||||
|
||||
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.
|
||||
const cancelTimeMs = 50;
|
||||
The cancellation was scheduled to occur after a delay of cancelTimeMs (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> fn = (x1, x2) => x1 * x2, args = [2,4], t = 30, cancelT = 100
|
||||
<strong>Input:</strong> fn = (x1, x2) => x1 * x2, args = [2,4], t = 30
|
||||
<strong>Output:</strong> [{"time": 30, "returned": 8}]
|
||||
<strong>Explanation:</strong>
|
||||
const cancel = cancellable((x1, x2) => x1 * x2, [2,4], 30); // fn(2,4) called at t=30ms
|
||||
setTimeout(cancel, 100);
|
||||
|
||||
The cancellation was scheduled to occur after a delay of cancelT (100ms), which happened after the execution of fn(2,4) at 30ms.
|
||||
<strong>Explanation:
|
||||
</strong>const cancelTimeMs = 100;
|
||||
The cancellation was scheduled to occur after a delay of cancelTimeMs (100ms), which happened after the execution of fn(2,4) at 30ms.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>fn is a function</code></li>
|
||||
<li><code>args is a valid JSON array</code></li>
|
||||
<li><code>fn</code> is a function</li>
|
||||
<li><code>args</code> is a valid JSON array</li>
|
||||
<li><code>1 <= args.length <= 10</code></li>
|
||||
<li><code><font face="monospace">20 <= t <= 1000</font></code></li>
|
||||
<li><code><font face="monospace">10 <= cancelT <= 1000</font></code></li>
|
||||
<li><code><font face="monospace">10 <= cancelTimeMs <= 1000</font></code></li>
|
||||
</ul>
|
||||
|
Reference in New Issue
Block a user