Given a function fn, an array of arguments args, and an interval time t, return a cancel function cancelFn. The function fn should be called with args immediately and then called again every t milliseconds until cancelFn is called. Example 1: Input: fn = (x) => x * 2, args = [4], t = 20, cancelT = 110 Output: [ {"time": 0, "returned": 8}, {"time": 20, "returned": 8}, {"time": 40, "returned": 8}, {"time": 60, "returned": 8}, {"time": 80, "returned": 8}, {"time": 100, "returned": 8} ] Explanation: Every 20ms, fn(4) is called. Until t=110ms, then it is cancelled. const cancel = cancellable(x => x * 2, [4], 20); setTimeout(cancel, 110); 1st fn call is at 0ms. fn(4) returns 8. 2nd fn call is at 20ms. fn(4) returns 8. 3rd fn call is at 40ms. fn(4) returns 8. 4th fn call is at 60ms. fn(4) returns 8. 5th fn call is at 80ms. fn(4) returns 8. 6th fn call is at 100ms. fn(4) returns 8.
时间: 2024-03-17 10:39:38 浏览: 11
Sure, here's an implementation in JavaScript:
```
function cancellable(fn, args, t) {
let time = 0;
let result = [];
const interval = setInterval(() => {
result.push({
time: time,
returned: fn(...args)
});
time += t;
}, t);
const cancelFn = () => {
clearInterval(interval);
return result;
}
return cancelFn;
}
```
You can use it like this:
```
const cancel = cancellable(x => x * 2, [4], 20);
setTimeout(() => {
const result = cancel();
console.log(result);
}, 110);
```
This will output the result array:
```
[
{"time": 0, "returned": 8},
{"time": 20, "returned": 8},
{"time": 40, "returned": 8},
{"time": 60, "returned": 8},
{"time": 80, "returned": 8},
{"time": 100, "returned": 8}
]
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)