第九条那个,因为你每次递归就建了两次工具函数和清理函数。其实把 print 函数再包一层就好了。 function printHello(delay) { let timer = null; // 执行函数包在内部,这样 timer 相对于它又是全局变量了 function fn() { console.log('hello'); timer = setTimeout(fn, 5000); } fn(); setTimeout(() => { if (!timer) return; clearTimeout(timer); console.log('clear'); }, delay); }
3 1

相关推荐

notbeentak...:真的nc,算毕业6月份,要给这种b公司打半年多白工😅
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务