-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2715-timeout-cancellation.js
More file actions
28 lines (27 loc) · 955 Bytes
/
2715-timeout-cancellation.js
File metadata and controls
28 lines (27 loc) · 955 Bytes
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
/**
* 2715. Timeout Cancellation
* https://leetcode.com/problems/timeout-cancellation/
* Difficulty: Easy
*
* Given a function fn, an array of arguments args, and a timeout t in milliseconds,
* return a cancel function cancelFn.
*
* After a delay of cancelTimeMs, the returned cancel function cancelFn will be invoked.
* > setTimeout(cancelFn, cancelTimeMs)
*
* Initially, the execution of the function fn should be delayed by t milliseconds.
*
* 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.
*/
/**
* @param {Function} fn
* @param {Array} args
* @param {number} t
* @return {Function}
*/
var cancellable = function(fn, args, t) {
const timeoutId = setTimeout(() => fn(...args), t);
return () => clearTimeout(timeoutId);
};