jQuery trigger custom event synchronously?
Posted
by
Miguel Angelo
on Stack Overflow
See other posts from Stack Overflow
or by Miguel Angelo
Published on 2012-11-12T04:41:49Z
Indexed on
2012/11/12
5:00 UTC
Read the original article
Hit count: 154
I am using the jQuery trigger method to call an event... but it behaves inconsistently. Sometimes it call the event, sometimes it does not.
<a href="#" onclick="
$(this).trigger('custom-event');
window.location.href = 'url';
return false;
">text</a>
The custom-event
has lots of listeners added to it.
It is as if the trigger method is not synchronous, allowing the window.location.href
be changed before executing the events. And when window.location.href is changed a navigation occurs, interrupting everything.
How can I trigger events synchronously?
Using jQuery 1.8.1.
Thanks!
EDIT
I have found that the event, when called has a stack trace like this:
- jQuery.fx.tick (jquery-1.8.1.js:9021)
- tick (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)
This proves that jQuery trigger
method is asynchronous.
Oohhh my... =\
© Stack Overflow or respective owner