js 闭包使用一例
有如下js:
function startTimer(sentence) { alert('start'); sentence.longPressTimer = setTimeout(function() { alert(sentence); //此处sentence为undefined }, 1000); } function endTimer(sentence) { alert('end'); clearTimeout(sentence.longPressTimer); } var sentences = document.querySelectorAll('span.sentence'); alert(sentences.length); for (var i = 0; i < sentences.length; i++) { sentences[i].addEventListener('touchstart', function(){ startTimer(sentences[i]); }); }
经过询问gpt3.5,成功解决了问题。回复如下:
根据你的代码,问题在于 sentences[i]
在回调函数中无法正确地获取到。这是因为回调函数是异步的,当它被调用时,循环已经结束了,此时 i
的值为 sentences.length
,导致无法正确获取到 sentences[i]
。
为了解决这个问题,我们可以使用闭包来创建一个独立的作用域,以便在每次迭代中捕获正确的 sentences[i]
值。可以通过将 sentences[i]
作为参数传递给一个立即执行的函数表达式来实现闭包。修正后的代码如下所示:
function startTimer(sentence) { alert('start'); sentence.longPressTimer = setTimeout(function() { alert(sentence); }, 1000); } function endTimer(sentence) { alert('end'); clearTimeout(sentence.longPressTimer); } var sentences = document.querySelectorAll('span.sentence'); for (var i = 0; i < sentences.length; i++) { (function(index) { sentences[index].addEventListener('touchstart', function() { startTimer(sentences[index]); }); })(i); }
在这个修正后的代码中,我们使用立即执行的函数表达式 (function(index) { ... })(i)
来创建一个闭包。index
参数接收了循环的当前索引值 i
,并在闭包内部被捕获。这样,在每次迭代中,都会创建一个新的独立作用域,并将正确的 sentences[i]
值传递给 startTimer()
函数。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2022-11-27 Cent 制作离线rpm包(包括所有依赖项)
2022-11-27 Cent 7 安装 tilde 文本编辑器
2022-11-27 Python基于pip实现离线打包