-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2805-custom-interval.js
More file actions
62 lines (57 loc) · 1.48 KB
/
2805-custom-interval.js
File metadata and controls
62 lines (57 loc) · 1.48 KB
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
/**
* 2805. Custom Interval
* https://leetcode.com/problems/custom-interval/
* Difficulty: Medium
*
* Function customInterval
*
* Given a function fn, a number delay and a number period, return a number id.
*
* customInterval is a function that should execute the provided function fn at intervals based
* on a linear pattern defined by the formula delay + period * count.
*
* The count in the formula represents the number of times the interval has been executed
* starting from an initial value of 0.
*
* Function customClearInterval
*
* Given the id. id is the returned value from the function customInterval.
*
* customClearInterval should stop executing provided function fn at intervals.
*
* Note: The setTimeout and setInterval functions in Node.js return an object, not a number.
*/
const intervalMap = new Map();
let nextId = 1;
/**
* @param {Function} fn
* @param {number} delay
* @param {number} period
* @return {number} id
*/
function customInterval(fn, delay, period) {
let count = 0;
const id = nextId++;
function scheduleNext() {
const nextDelay = delay + period * count;
const timeoutId = setTimeout(() => {
fn();
count++;
scheduleNext();
}, nextDelay);
intervalMap.set(id, timeoutId);
}
scheduleNext();
return id;
}
/**
* @param {number} id
* @return {void}
*/
function customClearInterval(id) {
const timeoutId = intervalMap.get(id);
if (timeoutId) {
clearTimeout(timeoutId);
intervalMap.delete(id);
}
}