1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/执行可取消的延迟函数 [execute-cancellable-function-with-delay].html
2023-06-12 23:05:37 +08:00

46 lines
1.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>现给定一个函数 <code>fn</code>&nbsp;,一个参数数组 <code>args</code> 和一个以毫秒为单位的超时时间 <code>t</code> ,返回一个取消函数 <code>cancelFn</code></p>
<p>在经过 <code>t</code> 毫秒的延迟后,<strong>除非</strong> 先调用 <code>cancelFn</code> ,否则&nbsp;<code>fn</code> 应该以 <code>args</code> 作为参数被调用。并且在这种情况下,<code>fn</code> 不应该被调用。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1:</strong></p>
<pre>
<b>输入:</b>fn = (x) =&gt; x * 5, args = [2], t = 20, cancelTime = 50
<b>输出:</b>[{"time": 20, "returned": 10}]
<b>解释:</b>
const cancel = cancellable(fn, [2], 20); // // 在 t=20ms 时调用 fn(2)
setTimeout(cancel, 50);
cancelTime50ms在延迟时间20ms之后所以 fn(2) 应该在 t=20ms 时调用。fn 的返回值是 10。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>fn = (x) =&gt; x**2, args = [2], t = 100, cancelTime = 50
<b>输出:</b>[]
<b>解释:</b>fn(2) 从未被调用,因为 cancelTime50ms在延迟时间100ms之前。
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30, cancelTime = 100
<b>输出:</b>[{"time": 30, "returned": 8}]
<b>解释:</b>fn(2) 从未被调用,因为 cancelTime50ms在延迟时间100ms之前。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>fn 是一个函数</code></li>
<li><code>args 是一个有效的 JSON 数组</code></li>
<li><code>1 &lt;= args.length &lt;= 10</code></li>
<li><code><font face="monospace">20 &lt;= t &lt;= 1000</font></code></li>
<li><code><font face="monospace">10 &lt;= cancelT &lt;= 1000</font></code></li>
</ul>