6

I think that event handlers are processed in the order that they are registered. (Is that correct?) If that is the case then if I attach an event handler at the beginning of my script, can I be absolutely certain that it will fire before subsequent handlers attached to the same event? Also do event namespaces have any effect on this? Are event handlers fired in series (one finishes before the next) or in parallel?

I want to do this because my script relies on the viewport size, which changes on the resize event, and I need to constantly look for it. Rather than calling $(window).width() repeatedly in each of my handler functions, I'd like to put a handler at the top of my script that saves $(window).width() to an object property on each resize. Then in subsequent handlers I can access the property and be certain that it is the most recent measurement.

I know I could create a custom event that triggers as soon as the calculation is done and attach subsequent handlers to the custom event. But, I'd don't want to do that because I need to maintain interoperability with plugins that come after mine that may also be attaching to the resize event.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ryanve
  • 50,076
  • 30
  • 102
  • 137
  • 1
    See http://stackoverflow.com/questions/290254/how-to-order-events-bound-with-jquery – ustun Jan 27 '12 at 17:04
  • Are you losing significant performance to `$(window).width()`? If not, why bother? You'll just be introducing a fragile dependency on the state of a global variable. – millimoose Jan 27 '12 at 17:05
  • @Inerdial I need to call it and some other calculations like 20 times. I think it will make a difference. – ryanve Jan 27 '12 at 17:08
  • 20 times? No it won't. Try executing the calculations you mention in the developer console in a loop with 20 iterations to see if you can notice a delay. (Better yet, try 2000.) – millimoose Jan 27 '12 at 17:18
  • @Inerdial Why repeat code if there's a way not to? The question is about making it not fragile. I also think there will be a minification benefit. It's more calculations than simply `$(window).width()` - calls to functions that need that. – ryanve Jan 27 '12 at 17:41

1 Answers1

4

If you attach your event handler before any other script has run (even just a plugin), then your callback will always fire first.

You can inspect the array of event handlers through the data object:

$(window).data('events');

Note: this is a non-documented approach and might not work eventually. Use it for inspection only.

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • sorry for the aside, is there a way to access that with pure Javascript? – André Alçada Padez Jan 27 '12 at 17:07
  • 3
    @AndréAlçadaPadez - Nope. Not only that, this'll only give you event handlers that you attached using jQuery. – Joseph Silber Jan 27 '12 at 17:08
  • 1
    thanks, i am having a lot of trouble with javascript events :) – André Alçada Padez Jan 27 '12 at 17:09
  • @AndréAlçadaPadez Yea events are confusing IMO. There's a lot of methods for them http://api.jquery.com/category/events/event-handler-attachment/ The docs for `$.on()` give some insight on when to use which one. Also this article is worth reading to understand what `trigger` does: http://net.tutsplus.com/tutorials/javascript-ajax/custom-events-and-special-events-api-in-jquery/ – ryanve Jan 27 '12 at 19:27
  • Thanks but i really need it in pure javascript :) – André Alçada Padez Jan 27 '12 at 19:47
  • FYI does not work in firefox 29.0.1: `>>> $(window).data('events');` gives `undefined`, too bad I really wanted to see that... – user9645 May 16 '14 at 17:00
  • Doh missed the edit window by less than a minute... since jQuery 1.8 you need to do: `$._data(window, 'events');`, see [this](http://stackoverflow.com/a/12214688/1571426) posting. – user9645 May 16 '14 at 17:07