Variable scope jQuery event with consecutive calls

When delegating events in jQuery 1.6, you can respond to events that bubble up to the common parent.

An example implementation is 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);

Such a structure can work very well when you have many event triggers that you want to link on the page. However, I noticed curious behavior and wondered if anyone could enlighten me in the area of ​​the following:

If you define the variable ' var bar;' before 'a' and use it in 'a', and 'a' takes a long time, then if 'click' is run a second time, will the string 'variable reassign the value or will a new one be created?

, "" , 1,3 . , , , .

, , jsFiddle, - , , - , ?

EDIT: , , , . "bar" - , (-), , , , , , , . - , . , jsFiddle, .

, AE

+5
2

, bar, . , fiddle. , , , , .

+1

, , "a" , .

0

All Articles