tags:

views:

46

answers:

1

Hi,

I have a function that does the following:

  1. binds all images on the page to a click handler.
  2. when clicked, unbinds then makes a ajax request
  3. binds the link again.

Theoretically this should work correct? (sorry don't have the code with me)

+2  A: 

Could you provide more information? Why do you unbind the event handlers? Is this just to prevent one from clicking again and getting a second ajax call, while the first is still running? If so, I would do it differently: Bind the handler to the images and leave them binded. If someone clicks an image, your handler gets called. The handler then starts an ajax request and sets a flag (e.g. loading = true). If the images are clicked again, the function ignores that as long as loading is true. If the request is done, you reset your loading-flag to false.

This technique is often used and should be faster then walking over the DOM, find the elements bind event handlers and so on. As a side effect, you now have a loading flag that could be used to display a throbber while loading.

Tim Büthe
I actually have seen that done, not sure why I didn't remember! thanks allot!
Blankman