题目来源:
leetcode题目,网址:2715. 执行可取消的延迟函数 - 力扣(LeetCode)
解题思路:
不懂,抄的题解。
解题代码:
/*** @param {Function} fn* @param {Array} args* @param {number} t* @return {Function}*/
var cancellable = function(fn, args, t) {let timer=setTimeout(()=>fn(...args),t);return ()=>{clearTimeout(timer);}
};/*** const result = []** const fn = (x) => x * 5* const args = [2], t = 20, cancelT = 50** const start = performance.now() ** const log = (...argsArr) => {* const diff = Math.floor(performance.now() - start);* result.push({"time": diff, "returned": fn(...argsArr)})* }* * const cancel = cancellable(log, args, t);** const maxT = Math.max(t, cancelT)* * setTimeout(() => {* cancel()* }, cancelT)** setTimeout(() => {* console.log(result) // [{"time":20,"returned":10}]* }, maxT + 15)*/
总结:
执行逻辑不是很懂。
(a)=> fn(b) 等价于function(a){return fn(b)}