带有事件参数的Javascript setTimeout 函数调用?
使用 setTimeout 时拉入事件对象的最佳方法是什么?我正在使用 jQuery 来处理所有浏览器中事件模型的标准化,但我不确定如何将“e”对象放入 checkPos 函数中。
我当前的代码:
function MouseDownEvent(e) {
*snip*
timeoutID = setTimeout(checkPos(e), 500);
}
function checkPos(e) {
//function uses e on a timeout of 500ms
timeoutID = setTimeout( checkPos(e) }, 500);
}
目前该代码可以工作一次,因为该函数是在 mousedown 事件中调用的,但在用户移动鼠标时永远不会更新 e 对象。 FF javascript 错误控制台还声明它是“无用的 setTimeout 调用(参数周围缺少引号?)”,但遵循该建议会导致它完全失败。
如何从 setTimeout 调用中提取“e”事件参数?
编辑:添加到每 500 毫秒重新运行 checkPos 函数的代码中
What is the best way to pull in the event object when using setTimeout? I'm using jQuery to handle normalizing the event model in all browsers, but I'm not sure how to get the 'e' object in to the checkPos function.
My current code:
function MouseDownEvent(e) {
*snip*
timeoutID = setTimeout(checkPos(e), 500);
}
function checkPos(e) {
//function uses e on a timeout of 500ms
timeoutID = setTimeout( checkPos(e) }, 500);
}
Currently that code works once because the function is called in the mousedown event, but never updates the e object as the user moves the mouse. The FF javascript error console also declares that it is 'a useless setTimeout call (missing quotes around argument?)', but following that advice causes it to completely fail.
How can I pull in the 'e' event argument from a setTimeout call?
Edit: Added in the code that reruns the checkPos function every 500ms
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试:
根据OP评论进行编辑..
要在每次触发checkPos时访问更新的事件:
Try:
EDIT due to OP comments..
To have access to an updated event each time checkPos is fired:
首先,为什么要使用两次超时?在我看来, setInterval() 会更好
其次,您能否澄清这一点:“...但永远不会在用户移动鼠标时更新 e 对象。”为什么当用户移动鼠标时事件对象会更新?您只分配了一个 mouseDown 处理程序。如果您想在每次鼠标移动时执行某些操作,那么您应该使用 mouseMove 事件,在这种情况下,超时/间隔无论如何都是不必要的。
与 JavaScript 中一样,您应该首先寻找事件驱动的解决方案,并且仅在绝对必要时才使用定时处理程序。
*编辑-解决评论中提出的问题*
Firstly why are you using two timeouts? It looks to me like setInterval() would be better
Secondly could you please clarify this : "...but never updates the e object as the user moves the mouse." Why would the event object be updated when the user moves the mouse? You've only assigned a mouseDown handler. If you wanted to do something on every mouse move then you should use a mouseMove event, in which case the timeout / interval would be unnecessary anyway.
As always in JavaScript you should look for an event driven solution first and only use timed handlers when you absolutely have to.
*Edit - addressing issues op raised in comments *
您可以使用 curry 函数,此处有一个用于 jQuery 的函数。
一般来说,你会这样:
所以你可以这样做:
因为 curry 返回一个函数,其第一个参数绑定到 e,所以你可以将它直接传递到 setTimeout 中。
You could use a curry function, there is one for jQuery here.
Generally, you would have this:
so you could do:
Since curry returns a function with it's first argument bound to e, you can pass it straight into setTimeout.
据我所知,IE 不允许按照您的需要传递“事件”对象。
我能想到的唯一解决方法是提前将您需要的任何值存储为全局变量,然后在延迟函数中检查该变量。
例如: http://jsfiddle.net/YvYtn/1/
这将显示最后一个 X 位置,您可以从事件对象中存储您需要的任何内容。
JS代码:
As far as I could see, IE won't allow to pass the "event" object around like you need.
The only workaround I can think of is storing whatever value you need in advance as global variable then check that variable in the delayed function.
For example: http://jsfiddle.net/YvYtn/1/
This will show the last X position, you can store whatever you need from the event object.
The JS code: