mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-12 19:01:47 +08:00
批量更新数据
This commit is contained in:
@@ -1,40 +1,53 @@
|
||||
<p>现给定一个函数 <code>fn</code> ,一个参数数组 <code>args</code> 和一个以毫秒为单位的超时时间 <code>t</code> ,返回一个取消函数 <code>cancelFn</code> 。</p>
|
||||
<p>给定一个函数 <code>fn</code> ,一个参数数组 <code>args</code> 和一个以毫秒为单位的超时时间 <code>t</code> ,返回一个取消函数 <code>cancelFn</code> 。</p>
|
||||
|
||||
<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>
|
||||
<p>在 <code>cancelTimeMs</code> 的延迟后,返回的取消函数 <code>cancelFn</code> 将被调用。</p>
|
||||
|
||||
<pre>
|
||||
setTimeout(cancelFn, cancelTimeMs)
|
||||
</pre>
|
||||
|
||||
<p>最初,函数 <code>fn</code> 的执行应该延迟 <code>t</code> 毫秒。</p>
|
||||
|
||||
<p>如果在 <code>t</code> 毫秒的延迟之前调用了函数 <code>cancelFn</code>,它应该取消 <code>fn</code> 的延迟执行。否则,如果在指定的延迟 <code>t</code> 内没有调用 <code>cancelFn</code>,则应执行 <code>fn</code>,并使用提供的 <code>args</code> 作为参数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>fn = (x) => x * 5, args = [2], t = 20, cancelT = 50
|
||||
<b>输入:</b>fn = (x) => x * 5, args = [2], t = 20
|
||||
<b>输出:</b>[{"time": 20, "returned": 10}]
|
||||
<b>解释:</b>
|
||||
const cancel = cancellable((x) => x * 5, [2], 20); // fn(2) 在 t=20ms 时被调用
|
||||
setTimeout(cancel, 50);
|
||||
const cancelTimeMs = 50;
|
||||
const cancelFn = cancellable((x) => x * 5, [2], 20);
|
||||
setTimeout(cancelFn, cancelTimeMs);
|
||||
|
||||
取消操作被安排在延迟了 cancelT(50毫秒)后进行,这发生在 fn(2) 在20毫秒时执行之后。</pre>
|
||||
取消操作被安排在延迟了 cancelTimeMs(50毫秒)后进行,这发生在 fn(2) 在20毫秒时执行之后。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>fn = (x) => x**2, args = [2], t = 100, cancelT = 50
|
||||
<b>输入:</b>fn = (x) => x**2, args = [2], t = 100
|
||||
<b>输出:</b>[]
|
||||
<b>解释:</b>
|
||||
const cancel = cancellable((x) => x**2, [2], 100); // fn(2) 没被调用
|
||||
setTimeout(cancel, 50);
|
||||
const cancelTimeMs = 50;
|
||||
const cancelFn = cancellable((x) => x**2, [2], 100);
|
||||
setTimeout(cancelFn, cancelTimeMs);
|
||||
|
||||
取消操作被安排在延迟了 cancelT(50毫秒)后进行,这发生在 fn(2) 在100毫秒时执行之前,导致 fn(2) 从未被调用。
|
||||
取消操作被安排在延迟了 cancelTimeMs(50毫秒)后进行,这发生在 fn(2) 在100毫秒时执行之前,导致 fn(2) 从未被调用。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>fn = (x1, x2) => x1 * x2, args = [2,4], t = 30, cancelTime = 100
|
||||
<b>输入:</b>fn = (x1, x2) => x1 * x2, args = [2,4], t = 30
|
||||
<b>输出:</b>[{"time": 30, "returned": 8}]
|
||||
<b>解释:</b>
|
||||
const cancel = cancellable((x1, x2) => x1 * x2, [2,4], 30); // fn(2,4) 在 t=30ms 时被调用
|
||||
setTimeout(cancel, 100);
|
||||
const cancelTimeMs = 100;
|
||||
const cancelFn = cancellable((x1, x2) => x1 * x2, [2,4], 30);
|
||||
setTimeout(cancelFn, cancelTimeMs);
|
||||
|
||||
取消操作被安排在延迟了 cancelT(100毫秒)后进行,这发生在 fn(2,4) 在30毫秒时执行之后。
|
||||
取消操作被安排在延迟了 cancelTimeMs(100毫秒)后进行,这发生在 fn(2,4) 在30毫秒时执行之后。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
@@ -46,5 +59,5 @@ setTimeout(cancel, 100);
|
||||
<li><code>args</code> 是一个有效的 JSON 数组</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