在函数上执行 setTimeout 时出现问题 - 将其作为参数传递
大家好,我有一个函数接受 this
作为参数 - 'this
' 指的是 dom 元素,单击该元素后应该运行一个函数。问题是,我希望在一小段延迟后调用此函数,但是传递变量项 this
不起作用,因为当执行函数“this
”时,则不会不是指传入参数中的对象,而是指窗口对象。
我怎样才能完成这件事?
Hi guys I have a function which accepts this
as a parameter - 'this
' referring to the dom element which upon clicked should run a function. The thing is that I want this function to be called after a small delay however passing the variable term this
doesn't work as when the function is executed 'this
' then doesn't refer to the object in passed in the parameter but to the window object.
How can I get this done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以捕获
这个
:You could capture
this
:PrototypeJS 将 bind() 方法添加到 函数.原型。此方法允许您将函数和参数绑定到特定对象的上下文。简而言之,
最好的部分是,该方法现在已成为 ECMA-262 规范的一部分,JavaScript 正是基于该规范,并且本机实现正在推广到现代浏览器中。如果尚未实现,PrototypeJS 只会添加此方法。
我在 http://jsfiddle.net/rLpbx/ 处设置了一个示例脚本。
PrototypeJS adds the bind() method to Function.prototype. This method allows you to bind a function and arguments to the context of a particular object. Simply,
The best part is that this method is now part of the ECMA-262 specification, which JavaScript is based upon, and native implementations are rolling out into modern browsers. PrototypeJS will only add this method if it's not already implemented.
I've set up an example script at http://jsfiddle.net/rLpbx/.