Skip to content

Commit 0a0b279

Browse files
day 15 task complete
1 parent 436d232 commit 0a0b279

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

day15/leetcode2725.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
*/

0 commit comments

Comments
 (0)