Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery event delegation variable scope with consecutive calls
    primarykey
    data
    text
    <p>With event delegation in jQuery 1.6, you can respond to events that have bubbled up to a common parent.</p> <p>A sample implementation is like this:</p> <pre><code>(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); </code></pre> <p>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:</p> <p>If you define a variable '<code>var bar;</code>' 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?</p> <p>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.</p> <p>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?</p> <p><strong>EDIT:</strong> <em>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.</em></p> <p>Thanks, AE</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload