mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-12 02:41:42 +08:00
批量更新数据
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
<p>Given a function <code>fn</code>, an array of arguments <code>args</code>, and an interval time <code>t</code>, return a cancel function <code>cancelFn</code>.</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>The function <code>fn</code> should be called with <code>args</code> immediately and then called again every <code>t</code> milliseconds until <code>cancelFn</code> is called at <code>cancelTimeMs</code> ms.</p>
|
||||
|
||||
<p> </p>
|
||||
@@ -17,23 +23,9 @@
|
||||
{"time": 175, "returned": 8}
|
||||
]
|
||||
<strong>Explanation:</strong>
|
||||
const result = [];
|
||||
const fn = (x) => x * 2;
|
||||
const cancelTimeMs = 190;
|
||||
|
||||
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, [4], 35);
|
||||
setTimeout(cancel, cancelTimeMs);
|
||||
|
||||
setTimeout(() => {
|
||||
console.log(result); // Output
|
||||
}, cancelTimeMs + 50)
|
||||
const cancelFn = cancellable((x) => x * 2, [4], 35);
|
||||
setTimeout(cancelFn, cancelTimeMs);
|
||||
|
||||
Every 35ms, fn(4) is called. Until t=190ms, then it is cancelled.
|
||||
1st fn call is at 0ms. fn(4) returns 8.
|
||||
@@ -59,7 +51,9 @@ Cancelled at 190ms
|
||||
{"time": 150, "returned": 10}
|
||||
]
|
||||
<strong>Explanation:</strong>
|
||||
const cancelTimeMs = 165;
|
||||
const cancelTimeMs = 165;
|
||||
const cancelFn = cancellable((x1, x2) => (x1 * x2), [2, 5], 30)
|
||||
setTimeout(cancelFn, cancelTimeMs)
|
||||
|
||||
Every 30ms, fn(2, 5) is called. Until t=165ms, then it is cancelled.
|
||||
1st fn call is at 0ms
|
||||
@@ -84,6 +78,8 @@ Cancelled at 165ms
|
||||
]
|
||||
<strong>Explanation:</strong>
|
||||
const cancelTimeMs = 180;
|
||||
const cancelFn = cancellable((x1, x2, x3) => (x1 + x2 + x3), [5, 1, 3], 50)
|
||||
setTimeout(cancelFn, cancelTimeMs)
|
||||
|
||||
Every 50ms, fn(5, 1, 3) is called. Until t=180ms, then it is cancelled.
|
||||
1st fn call is at 0ms
|
||||
|
Reference in New Issue
Block a user