views:

18

answers:

1

Hi,

I have a script which adds click event handlers to about 500 elements in the page via JQuery .live or .bind (not decided which yet), and I am having a JQuery performance issue.

With a small dataset, I can load the page and as soon as the page has finished loading according to the browser, I can click on something and off we go. But increase the data set and there is a definite delay between the page load ending and my click being handled, which I can only assume is JQuery still working off attaching the event handlers - the click is handled correctly, so I am assuming it gets queued and then handled.

Is there any way to have JQuery wait for the .live() to finish before moving on, or in other words handle it synchronously rather than asynchronously if that makes sense? This is so I can put a loading screen up before starting the .live() call, and take it down when its done.

Regards Moo

A: 

A jQuery .live() handler (or .delegate()) should finish very quickly, unlike .bind() which binds each element. In these situations you should use .live() or .delegate(), since the startup cost of .bind() far exceeds the event bubbling/listening cost of a handler on a parent element.

Nick Craver
The delay happens whether I use .bind or .live, and I cannot see any other reason for the delay other than these particular calls. Basically, my app starts up on $(document).ready, calls $('.MenuItem').live(..... and then sits there waiting for user interaction. With a small amount of test data, the app is instantly usable, but increase the test data and I get the aforementioned delay in handling the first click.
Moo
@Moo - `.live()` wouldn't cause your delay, not if properly used...there must be something else going on, `live()` creates one event handler on `document`, that's literally *all* it does, what else is happening besides the bind?
Nick Craver
At this point? Literally nothing - the entire rest of the app works off of these bound click events, there is no dom manipulation going on, no animations, no nothing until a click happens.
Moo
@Moo - Any chance you can give me a link? Can take a look real quick and see if there's anything weird that I notice
Nick Craver
Not publically really, do you have an email address I could send one to?
Moo