5

With event delegation in jQuery 1.6, you can respond to events that have bubbled up to a common parent.

A sample implementation is like this:

(function($){
  $.fn.myeventhandler = function () {

     return this.delegate('a.next, a.prev', 'click customEvent', function (event) {

       var target=$(event.target);
       var bar = null;

       if('click' == event.type) {              /*~[ call this 'a' ]~*/

          // handle the click event
          $('#next-element').animate({width:'200px'},1300,function(){
             bar = 'value1';
             $('#next-element').animate({width:'100px'},1300,function(){
                 bar = 'value2';
                 console.log(bar);
             });
          });

       } else if('customEvent' == event.type) { /*~[ call this 'b' ]~*/

          // handle the customEvent event
          $('#my-element').animate({height:'200px'},1300,function(){
             bar = 'my-event1';
             console.log(bar);
          });

       }

       return false;
     });
  }
})(jQuery);

This kind of structure can work really well when you have many event triggers you want to have bound on a page. However, I've noticed a curious behaviour and wondered if someone could enlighten me on the scope of the following:

If you define a variable 'var bar;' before 'a' and use it in 'a' and 'a' takes a long time to execute, then if 'click' is triggered a second time, would the 'bar' variable be reassigned the value or would a new one be created?

The reason for the question is that I've got an 'unfinished' animation in which the animation takes 1.3 seconds to complete. If I trigger two consecutive click events, the target and type are correctly set, but the second animation appears to be affecting the first one and the two scopes appear to be available to each other.

I haven't yet finished the animation, so I haven't got a working set to put online in jsFiddle, but is there any rule of thumb for scope here that I should be researching or some clearer explanation of scope available someone may have found?

EDIT: In the above, I've added in a situation similar to what I'm using. In the simplified case above, the value of 'bar' still holds the expected value on 2 consecutive calls, but in the more complicated case I am working through (still), unless I pass the parameters into the callback function specifically, there is still a condition that's allowing 2 consecutive calls to change the value of the parameter such that the callback sees a value created by a subsequent call - not the value that was logically set in its own triggered call. If anyone can replicate this effect on a simplified example, I'd appreciate it. I'm whittling down my functions to try to produce a jsFiddle that replicates it.

Thanks, AE

MyStream
  • 2,533
  • 1
  • 16
  • 33
  • Hi, Interstellar_Coder, 'a' in the comments refers to the first if() block of code and 'b' refers to the 'else' block of code - sorry if the comments were unhelpful - what's a good example to illustrate it better? – MyStream Sep 30 '11 at 14:21

2 Answers2

1

If you click it a second time then a new bar variable would be created, both bar values would be independent of each other. To see this in action i created a fiddle. Click twice fast and you will notice that the second time bar takes the default value and not the value that the initial bar variable had, which is expected.

aziz punjani
  • 25,586
  • 9
  • 47
  • 56
  • This is the a good example (slightly edited to match the question more closely with no external variable scope) of how one call should not affect another. I have yet to see what I've done to cause it, but it's looking more like an error than a scope issue. Can you expand on the scope itself and explain at all what happens on each event trigger? Is it self-contained? – MyStream Sep 30 '11 at 16:02
0

The event object is passed around instead of being newly created with various events, therefore, 'a' would not be re-assigned, but would be affected by both event actions.

swatkins
  • 13,530
  • 4
  • 46
  • 78
  • Hi @swatkins, I'm not sure I followed what you meant. It's interesting in that the http://jsfiddle.net/BHaEj/4/ link (altered) it shows that the bar variable isn't blitzed when the second event comes in, and in the code I'm still not finished with, I can affect the variables in if loops in subsequent calls. I'll have another go at my variable names and see if there's a separate issue at play. – MyStream Sep 30 '11 at 16:00
  • Well, in the fiddle, you have `count` in the global space. So it's not tied to the event object at all - the event object is modifying `count`, and that's why this fiddle is working as expected. However, if you declare `count` within the callback function of the click action, then it would be re-declared each time a click happens. The event object itself is the same (with different values for properties), but the particular variables declared would be re-declared in each action. Here's a look a the docs for the jQuery Event Object : http://api.jquery.com/category/events/event-object/ – swatkins Sep 30 '11 at 17:55