Skip to content

Commit 5a18fd5

Browse files
committed
Add solution #2725
1 parent 355a388 commit 5a18fd5

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@
488488
2722|[Join Two Arrays by ID](./2722-join-two-arrays-by-id.js)|Medium|
489489
2723|[Add Two Promises](./2723-add-two-promises.js)|Easy|
490490
2724|[Sort By](./2724-sort-by.js)|Easy|
491+
2725|[Interval Cancellation](./2725-interval-cancellation.js)|Easy|
491492
2726|[Calculator with Method Chaining](./2726-calculator-with-method-chaining.js)|Easy|
492493
2727|[Is Object Empty](./2727-is-object-empty.js)|Easy|
493494
3042|[Count Prefix and Suffix Pairs I](./3042-count-prefix-and-suffix-pairs-i.js)|Easy|
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 2725. Interval Cancellation
3+
* https://leetcode.com/problems/interval-cancellation/
4+
* Difficulty: Easy
5+
*
6+
* Given a function fn, an array of arguments args, and an interval time t, return a cancel
7+
* function cancelFn.
8+
*
9+
* After a delay of cancelTimeMs, the returned cancel function cancelFn will be invoked.
10+
* > setTimeout(cancelFn, cancelTimeMs)
11+
*
12+
* The function fn should be called with args immediately and then called again every t
13+
* milliseconds until cancelFn is called at cancelTimeMs ms.
14+
*/
15+
16+
/**
17+
* @param {Function} fn
18+
* @param {Array} args
19+
* @param {number} t
20+
* @return {Function}
21+
*/
22+
var cancellable = function(fn, args, t) {
23+
fn(...args);
24+
const timer = setInterval(() => fn(...args), t);
25+
return () => clearInterval(timer);
26+
};

0 commit comments

Comments
 (0)