File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ function cancellable(fn: Function, args: any[], t: number): Function {
2+ fn(...args);
3+ const id = setInterval(() => {
4+ fn(...args);
5+ }, t)
6+ const cancelFn = () => clearInterval(id);
7+ return cancelFn;
8+ };
9+
10+ /**
11+ * const result = []
12+ *
13+ * const fn = (x) => x * 2
14+ * const args = [4], t = 35, cancelT = 190
15+ *
16+ * const start = performance.now()
17+ *
18+ * const log = (...argsArr) => {
19+ * const diff = Math.floor(performance.now() - start)
20+ * result.push({"time": diff, "returned": fn(...argsArr)})
21+ * }
22+ *
23+ * const cancel = cancellable(log, args, t);
24+ *
25+ * setTimeout(() => {
26+ * cancel()
27+ * }, cancelT)
28+ *
29+ * setTimeout(() => {
30+ * console.log(result) // [
31+ * // {"time":0,"returned":8},
32+ * // {"time":35,"returned":8},
33+ * // {"time":70,"returned":8},
34+ * // {"time":105,"returned":8},
35+ * // {"time":140,"returned":8},
36+ * // {"time":175,"returned":8}
37+ * // ]
38+ * }, cancelT + t + 15)
39+ */
You can’t perform that action at this time.
0 commit comments