-
Notifications
You must be signed in to change notification settings - Fork 0
/
intervalCancellation.js
99 lines (77 loc) · 2.65 KB
/
intervalCancellation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// LeetCode #2725. Interval Cancellation
/* Instructions
Given a function fn, an array of arguments args, and an interval time t, return a cancel function cancelFn. The function fn should be called with args immediately and then called again every t milliseconds until cancelFn is called.
Example 1:
Input: fn = (x) => x * 2, args = [4], t = 20, cancelT = 110
Output:
[
{"time": 0, "returned": 8},
{"time": 20, "returned": 8},
{"time": 40, "returned": 8},
{"time": 60, "returned": 8},
{"time": 80, "returned": 8},
{"time": 100, "returned": 8}
]
Explanation: Every 20ms, fn(4) is called. Until t=110ms, then it is cancelled.
const cancel = cancellable(x => x * 2, [4], 20);
setTimeout(cancel, 110);
1st fn call is at 0ms. fn(4) returns 8.
2nd fn call is at 20ms. fn(4) returns 8.
3rd fn call is at 40ms. fn(4) returns 8.
4th fn call is at 60ms. fn(4) returns 8.
5th fn call is at 80ms. fn(4) returns 8.
6th fn call is at 100ms. fn(4) returns 8.
Example 2:
Input: fn = (x1, x2) => (x1 * x2), args = [2, 5], t = 25, cancelT = 140
Output:
[
{"time": 0, "returned": 10},
{"time": 25, "returned": 10},
{"time": 50, "returned": 10},
{"time": 75, "returned": 10},
{"time": 100, "returned": 10},
{"time": 125, "returned": 10}
]
Explanation: Every 25ms, fn(2, 5) is called. Until t=140ms, then it is cancelled.
1st fn call is at 0ms
2nd fn call is at 25ms
3rd fn call is at 50ms
4th fn call is at 75ms
5th fn call is at 100ms
6th fn call is at 125ms
Cancelled at 140ms
Example 3:
Input: fn = (x1, x2, x3) => (x1 + x2 + x3), args = [5, 1, 3], t = 50, cancelT = 180
Output:
[
{"time": 0, "returned": 9},
{"time": 50, "returned": 9},
{"time": 100, "returned": 9},
{"time": 150, "returned": 9}
]
Explanation: Every 50ms, fn(5, 1, 3) is called. Until t=180ms, then it is cancelled.
1st fn call is at 0ms
2nd fn call is at 50ms
3rd fn call is at 100ms
4th fn call is at 150ms
Cancelled at 180ms
Constraints:
fn is a function
args is a valid JSON array
1 <= args.length <= 10
20 <= t <= 1000
10 <= cancelT <= 1000
*/
// Solution
const cancellable = function(fn, args, t) {
// call the given function and its arguments
fn(...args)
// create a timer that calls the function and its arguments, every t (milliseconds)
let timer = setInterval(() => fn(...args), t)
// clear the timer of the tracked interval index.
let cancelFn = () => clearInterval(timer)
return cancelFn
}
// Explanation
// fn(...args) has to be called once before setting the interval, because setInterval() does not initially call the function.
// the setInterval() function is called when the `timer` variable is assigned its value.