如何通过“setInterval”有效地传递范围
我目前想知道是否有比通过参数 'e' 将 this 范围传递给 lambda 函数,然后使用 call 将其传递给 'funkyFunction' 更好的解决方案()-方法
setInterval(function(e){e.funkyFunction.call(e)}, speed, this)
(小问题除外:我一直在阅读有关 javascript 中内存泄漏的内容。lambda 函数如何影响我的记忆?最好先定义它,例如 var i = function(e) ...
然后将其作为参数传递给 setInterval?)
I'm currently wondering if there is a better solution than passing this scope to the lambda-function via the parameter 'e' and then passing it to 'funkyFunction' using call()-method
setInterval(function(e){e.funkyFunction.call(e)}, speed, this)
(Minor question aside: I'd been reading something about memory-leaks in javascript. How does the lambda-function affect my memory? Is it better to define it first like var i = function(e)...
and then passing it as a parameter to setInterval?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我的情况可能有点不同,但这就是我所做的:
我的情况是我的代码位于类方法内,我需要保持正确的范围,因为我不希望“this”绑定解析为当前窗口。
例如。我想使用 setInterval 从 MyClass.init 运行 MyClass.animate,因此我将此范围保持代码放入 MyClass.init 中
My situation may have been a bit different, but here's what I did:
My scenario was that my code was inside a class method and I needed to keep correct scope as I didn't want the 'this' binding to resolve to the current window.
eg. I wanted to run MyClass.animate from MyClass.init using setInterval so I put this scope-keep code into MyClass.init
您可以使用本机绑定功能。
将此示例粘贴到控制台中以查看结果
You can use native bind function.
paste this example in the console to see the result
简单地依赖外部作用域定义的变量有什么问题?
What's wrong with simply relying on the outer-scope defined variable?
我有同样的问题,但似乎没有内置的解决方案,所以这里是我一起拼凑的一个快速解决方法:
用法:
这仅涵盖传递实际函数的用例,而不是传递实际函数的用例要执行的一串代码。
至于使用此功能时有关内存泄漏的问题:与其说是使用 setInterval 的问题,不如说是使用匿名函数本身的问题。
如果您在 lambda 内使用对对象的引用,则只要匿名函数存在,该引用就会将引用的对象保留在内存中。我认为该函数是通过调用
clearInterval
来销毁的。我认为首先将函数分配给变量没有任何好处,相反,它会创建另一个包含引用的变量,只要 anon func 存在,该引用就不会被垃圾收集......
I had the same question, but there seems to be no built in solution, so here is a quick workaround I punched together:
usage:
This only covers the use-case where you pass an actual function, not a string of code to be executed.
As for your question about memory leaks when using this functionality: it is not so much the problem with using
setInterval
as it is with anonymous functions in itself.If you use a reference to an object inside a lambda, this reference will keep the referenced object in memory for as long as the anonymous function exists. I think the function is destroyed with a call to
clearInterval
.I don't think there is any benefit from assigning the function to a variable first, on the contrary, it will create another variable containing a reference that will not be garbage collected as long as the anon func exists...
您还可以查看
YUI
Framework
。它非常适合构建应用程序并且易于学习。UPDATE 为例,
使用 YUI 和 jQuery(不要忘记启用 $.noConflict())
简而言之,
请参阅 http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_later
更新
不需要 $.noConflict() 因为 YUI 不以任何方式使用 $。
You may also have a look at the
YUI
Framework
. It's fine for building applications and easy to learn.UPDATE as example
Using YUI and jQuery (Do not forget enable $.noConflict())
In short
see http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_later
UPDATE
No need for $.noConflict() because YUI does not use $ in any way.
有两个重要的区别需要做出。
1) 您是否想要对传递的参数进行引用,以便超时函数可以跟踪对其所做的更改,或者您是否想要传递的参数的克隆?
2) 您是否希望能够捕获对超时的引用,以防您想取消它? (是的!)
There are two important distinctions to make.
1) Do you want a reference to the passed parameter so that the timeout function can track changes made to it, or do you want a clone of the passed parameter?
2) Do you want to be able to capture a reference to the timeout in case you want to cancel it? (Yes!)