jquery callback functions failing to finish execution

Posted by calumbrodie on Stack Overflow See other posts from Stack Overflow or by calumbrodie
Published on 2010-06-15T14:58:41Z Indexed on 2010/06/15 15:02 UTC
Read the original article Hit count: 248

Filed under:
|
|
|

I'm testing a jquery app i've written and have come across some unexpected behaviour

$('button.clickme').live('click',function(){
    //do x (takes 2 seconds)
    //do y (takes 4 seconds)
    //do z (takes 0.5 seconds)
})

The event can be triggered by a number of buttons.

What I'm finding is that when I click each button slowly (allowing 10 seconds between clicks) - my callback function executes correctly (actions x, y & z complete).

However If I rapidly click buttons on my page it appears that the function sometimes only completes up to step x or y before terminating.

My question: Is it the case that if this function is fired by a clicking second DOM element, while the first callback function is completing - will jQuery terminate the first callback and start over again?

Do I have to write my callback function explicitly outside the event handler and then call it??

function doStuff() {
    //do x 
    //do y 
    //do z (
}

$('button.clickme).live('click',doStuff())

If this is the case can someone explain why this is happening or give me a link to some advice on best practice on closures etc - I'd like to know the BEST way to write jQuery to improve performance etc.

Thanks

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery