【发布时间】:2012-10-31 13:37:56
【问题描述】:
我正在使用 jQuery 触发方法来调用事件...但它的行为不一致。有时它调用事件,有时它不调用。
<a href="#" onclick="
$(this).trigger('custom-event');
window.location.href = 'url';
return false;
">text</a>
custom-event 添加了很多听众。
就好像触发方法不是同步的,允许在执行事件之前更改window.location.href。当 window.location.href 发生变化时,会发生导航,打断一切。
如何同步触发事件?
使用 jQuery 1.8.1。
编辑
我发现该事件在调用时具有如下堆栈跟踪:
- jQuery.fx.tick (jquery-1.8.1.js:9021)
- 打勾(jquery-1.8.1.js:8499)
- jQuery.Callbacks.self.fireWith (jquery-1.8.1.js:1082)
- jQuery.Callbacks.fire (jquery-1.8.1.js:974)
- jQuery.speed.opt.complete (jquery-1.8.1.js:8991)
- $.customEvent (myfile.js:28)
这证明 jQuery (我错了……这仅证明我正在调用的事件,其中有一个动画,并且正在调用预期的函数在动画之后的回调中)trigger 方法是异步的。
【问题讨论】:
标签: jquery synchronization jquery-events