开发者

jquery event delegation variable scope with consecutive calls

开发者 https://www.devze.com 2023-04-09 08:17 出处:网络
With event delegation in jQuery 1.6, you can respond to events that have bubbled up to a common parent.

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 cons开发者_如何学Pythonecutive 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消