输入: fn = (x) => x * 2, args = [4], t = 20, cancelT = 110 输出: [ {“time”: 0, “returned”: 8}, {“time”: 20, “returned”: 8}, {“time”: 40, “returned”: 8}, {“time”: 60, “returned”: 8}, {“time”: 80, “returned”: 8}, {“time”: 100, “returned”: 8} ]
每隔20s,都要进行4x2=8,直到110s时候取消。
var cancellable = function(fn, args, t) { fn(...args); const time = setInterval(() =>fn(...args),t); return () => clearInterval(time); };