Skip to content

Commit ae4425c

Browse files
authored
code added
1 parent 4f18f10 commit ae4425c

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

14_timeoutCancellation.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Question Link: https://leetcode.com/problems/timeout-cancellation/description/?envType=study-plan-v2&envId=30-days-of-javascript
2+
// Solution Link: https://leetcode.com/problems/timeout-cancellation/solutions/5439583/easy-javascript-solution/
3+
4+
/*
5+
2715. Timeout Cancellation
6+
7+
Given a function fn, an array of arguments args, and a timeout t in milliseconds, return a cancel function cancelFn.
8+
After a delay of cancelTimeMs, the returned cancel function cancelFn will be invoked.
9+
setTimeout(cancelFn, cancelTimeMs)
10+
11+
Initially, the execution of the function fn should be delayed by t milliseconds.
12+
If, before the delay of t milliseconds, the function cancelFn is invoked, it should cancel the delayed execution of fn. Otherwise, if cancelFn is not invoked within the specified delay t, fn should be executed with the provided args as arguments.
13+
14+
Example 1:
15+
Input: fn = (x) => x * 5, args = [2], t = 20
16+
Output: [{"time": 20, "returned": 10}]
17+
Explanation:
18+
const cancelTimeMs = 50;
19+
const cancelFn = cancellable((x) => x * 5, [2], 20);
20+
setTimeout(cancelFn, cancelTimeMs);
21+
The cancellation was scheduled to occur after a delay of cancelTimeMs (50ms), which happened after the execution of fn(2) at 20ms.
22+
23+
Example 2:
24+
Input: fn = (x) => x**2, args = [2], t = 100
25+
Output: []
26+
Explanation:
27+
const cancelTimeMs = 50;
28+
const cancelFn = cancellable((x) => x**2, [2], 100);
29+
setTimeout(cancelFn, cancelTimeMs);
30+
The cancellation was scheduled to occur after a delay of cancelTimeMs (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called.
31+
32+
Example 3:
33+
Input: fn = (x1, x2) => x1 * x2, args = [2,4], t = 30
34+
Output: [{"time": 30, "returned": 8}]
35+
Explanation:
36+
const cancelTimeMs = 100;
37+
const cancelFn = cancellable((x1, x2) => x1 * x2, [2,4], 30);
38+
setTimeout(cancelFn, cancelTimeMs);
39+
The cancellation was scheduled to occur after a delay of cancelTimeMs (100ms), which happened after the execution of fn(2,4) at 30ms.
40+
41+
Constraints:
42+
fn is a function
43+
args is a valid JSON array
44+
1 <= args.length <= 10
45+
20 <= t <= 1000
46+
10 <= cancelTimeMs <= 1000
47+
*/
48+
49+
50+
51+
/**
52+
* @param {Function} fn
53+
* @param {Array} args
54+
* @param {number} t
55+
* @return {Function}
56+
*/
57+
58+
var cancellable = function(fn, args, t) {
59+
const timeOver = setTimeout(() => fn(...args), t);
60+
61+
const cancelFn = () => clearTimeout(timeOver);
62+
63+
return cancelFn;
64+
};
65+
66+
/**
67+
* const result = [];
68+
*
69+
* const fn = (x) => x * 5;
70+
* const args = [2], t = 20, cancelTimeMs = 50;
71+
*
72+
* const start = performance.now();
73+
*
74+
* const log = (...argsArr) => {
75+
* const diff = Math.floor(performance.now() - start);
76+
* result.push({"time": diff, "returned": fn(...argsArr)});
77+
* }
78+
*
79+
* const cancel = cancellable(log, args, t);
80+
*
81+
* const maxT = Math.max(t, cancelTimeMs);
82+
*
83+
* setTimeout(cancel, cancelTimeMs);
84+
*
85+
* setTimeout(() => {
86+
* console.log(result); // [{"time":20,"returned":10}]
87+
* }, maxT + 15)
88+
*/

0 commit comments

Comments
 (0)