Skip to content

Commit 95fb8b2

Browse files
committed
Add solution #2715
1 parent baaee9d commit 95fb8b2

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@
407407
2677|[Chunk Array](./2677-chunk-array.js)|Easy|
408408
2695|[Array Wrapper](./2695-array-wrapper.js)|Easy|
409409
2703|[Return Length of Arguments Passed](./2703-return-length-of-arguments-passed.js)|Easy|
410+
2715|[Timeout Cancellation](./2715-timeout-cancellation.js)|Easy|
410411
2721|[Execute Asynchronous Functions in Parallel](./2721-execute-asynchronous-functions-in-parallel.js)|Medium|
411412
2722|[Join Two Arrays by ID](./2722-join-two-arrays-by-id.js)|Medium|
412413
2724|[Sort By](./2724-sort-by.js)|Easy|
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 2715. Timeout Cancellation
3+
* https://leetcode.com/problems/timeout-cancellation/
4+
* Difficulty: Easy
5+
*
6+
* Given a function fn, an array of arguments args, and a timeout t in milliseconds,
7+
* return a cancel function cancelFn.
8+
*
9+
* After a delay of cancelTimeMs, the returned cancel function cancelFn will be invoked.
10+
* > setTimeout(cancelFn, cancelTimeMs)
11+
*
12+
* Initially, the execution of the function fn should be delayed by t milliseconds.
13+
*
14+
* If, before the delay of t milliseconds, the function cancelFn is invoked, it should
15+
* cancel the delayed execution of fn. Otherwise, if cancelFn is not invoked within the
16+
* specified delay t, fn should be executed with the provided args as arguments.
17+
*/
18+
19+
/**
20+
* @param {Function} fn
21+
* @param {Array} args
22+
* @param {number} t
23+
* @return {Function}
24+
*/
25+
var cancellable = function(fn, args, t) {
26+
const timeoutId = setTimeout(() => fn(...args), t);
27+
return () => clearTimeout(timeoutId);
28+
};

0 commit comments

Comments
 (0)