Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b02ffaf

Browse files
2725 : README.md
1 parent a4034dd commit b02ffaf

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# 2725. Interval Cancellation
2+
3+
### Easy
4+
5+
---
6+
7+
Given a function `fn`, an array of arguments `args`, and an interval time `t`, return a cancel function `cancelFn`.
8+
After a delay of `cancelTimeMs`, the returned cancel function `cancelFn` will be invoked.
9+
10+
> setTimeout(cancelFn, cancelTimeMs)
11+
12+
The function `fn` should be called with `args` immediately and then called again every `t` milliseconds until `cancelFn` is called at `cancelTimeMs` ms.
13+
14+
---
15+
16+
### Example 1:
17+
18+
```javascript
19+
fn = (x) => x * 2, args = [4], t = 35
20+
/* Output:
21+
[
22+
{"time": 0, "returned": 8},
23+
{"time": 35, "returned": 8},
24+
{"time": 70, "returned": 8},
25+
{"time": 105, "returned": 8},
26+
{"time": 140, "returned": 8},
27+
{"time": 175, "returned": 8}
28+
] */
29+
```
30+
31+
**Explanation :**
32+
33+
const cancelTimeMs = 190;
34+
const cancelFn = cancellable((x) => x * 2, [4], 35);
35+
setTimeout(cancelFn, cancelTimeMs);
36+
37+
Every 35ms, fn(4) is called. Until t=190ms, then it is cancelled.
38+
1st fn call is at 0ms. fn(4) returns 8.
39+
2nd fn call is at 35ms. fn(4) returns 8.
40+
3rd fn call is at 70ms. fn(4) returns 8.
41+
4th fn call is at 105ms. fn(4) returns 8.
42+
5th fn call is at 140ms. fn(4) returns 8.
43+
6th fn call is at 175ms. fn(4) returns 8.
44+
Cancelled at 190ms
45+
46+
### Example 2:
47+
48+
```javascript
49+
fn = (x1, x2) => (x1 * x2), args = [2, 5], t = 30
50+
/* Output:
51+
[
52+
{"time": 0, "returned": 10},
53+
{"time": 30, "returned": 10},
54+
{"time": 60, "returned": 10},
55+
{"time": 90, "returned": 10},
56+
{"time": 120, "returned": 10},
57+
{"time": 150, "returned": 10}
58+
] */
59+
```
60+
61+
**Explanation :**
62+
63+
const cancelTimeMs = 165;
64+
const cancelFn = cancellable((x1, x2) => (x1 * x2), [2, 5], 30)
65+
setTimeout(cancelFn, cancelTimeMs)
66+
67+
Every 30ms, fn(2, 5) is called. Until t=165ms, then it is cancelled.
68+
1st fn call is at 0ms
69+
2nd fn call is at 30ms
70+
3rd fn call is at 60ms
71+
4th fn call is at 90ms
72+
5th fn call is at 120ms
73+
6th fn call is at 150ms
74+
Cancelled at 165ms
75+
76+
### Example 3:
77+
78+
```javascript
79+
fn = (x1, x2, x3) => (x1 + x2 + x3), args = [5, 1, 3], t = 50
80+
/* Output:
81+
[
82+
{"time": 0, "returned": 9},
83+
{"time": 50, "returned": 9},
84+
{"time": 100, "returned": 9},
85+
{"time": 150, "returned": 9}
86+
] */
87+
```
88+
89+
**Explanation :**
90+
91+
const cancelTimeMs = 180;
92+
const cancelFn = cancellable((x1, x2, x3) => (x1 + x2 + x3), [5, 1, 3], 50)
93+
setTimeout(cancelFn, cancelTimeMs)
94+
95+
Every 50ms, fn(5, 1, 3) is called. Until t=180ms, then it is cancelled.
96+
1st fn call is at 0ms
97+
2nd fn call is at 50ms
98+
3rd fn call is at 100ms
99+
4th fn call is at 150ms
100+
Cancelled at 180ms
101+
102+
### Constraints :
103+
104+
- `fn` is a function
105+
- `args` is a valid JSON array
106+
- `1 <= args.length <= 10`
107+
- `30 <= t <= 100`
108+
- `10 <= cancelTimeMs <= 500`
109+
110+
---

0 commit comments

Comments
 (0)