jquery callback functions failing to finish execution
- by calumbrodie
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