File tree 1 file changed +50
-0
lines changed
1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 2725. Interval Cancellation
2
+ // URL -> https://leetcode.com/problems/interval-cancellation
3
+
4
+ /**
5
+ * @param {Function } fn
6
+ * @param {Array } args
7
+ * @param {number } t
8
+ * @return {Function }
9
+ */
10
+ var cancellable = function ( fn , args , t ) {
11
+ fn ( ...args ) ;
12
+ let timer = setInterval ( ( ) => {
13
+ fn ( ...args ) ;
14
+ } , t )
15
+
16
+ return function ( ) {
17
+ clearInterval ( timer )
18
+ }
19
+ } ;
20
+
21
+ /**
22
+ * const result = []
23
+ *
24
+ * const fn = (x) => x * 2
25
+ * const args = [4], t = 35, cancelT = 190
26
+ *
27
+ * const start = performance.now()
28
+ *
29
+ * const log = (...argsArr) => {
30
+ * const diff = Math.floor(performance.now() - start)
31
+ * result.push({"time": diff, "returned": fn(...argsArr)})
32
+ * }
33
+ *
34
+ * const cancel = cancellable(log, args, t);
35
+ *
36
+ * setTimeout(() => {
37
+ * cancel()
38
+ * }, cancelT)
39
+ *
40
+ * setTimeout(() => {
41
+ * console.log(result) // [
42
+ * // {"time":0,"returned":8},
43
+ * // {"time":35,"returned":8},
44
+ * // {"time":70,"returned":8},
45
+ * // {"time":105,"returned":8},
46
+ * // {"time":140,"returned":8},
47
+ * // {"time":175,"returned":8}
48
+ * // ]
49
+ * }, cancelT + t + 15)
50
+ */
You can’t perform that action at this time.
0 commit comments