views:

41

answers:

1

Hey,

i am using the jquery toggle function like this:

$(".class").toggle( function() { //things to do... animations and so on }, function(){ // other things to do... } );

Now when I call the toggle effect by clicking very often the animations of the two functions don't wait for the other to finish. So I would like to disable the toggle effects undtil the stuff in each function is finished. So when the first function is called by the toggle the second one should not be called by another click on the .class element. And the other way round

Any idea how i could do that?

Thanks in advance, Phil

+2  A: 

You could add a .click() handler bound before the toggle that does this, for example:

$(".class").click(function() {
  if($(".selector:animated").length) 
    return false;
}).toggle(function() { 
  //things to do... animations and so on 
}, function(){
  // other things to do... 
});

What this does is if the check for your selector and :animated found any elements (they're still animating), it'd return false and prevent the .toggle() handler from even getting hit. This works because event handlers are executed in the order they were bound, so if the first one returns false, the second won't run.

Nick Craver
Thanks for the tip on the :animated pseudo selector. Didn't know that was possible. I used to implement .queue() but :animated seems more suited for small animations.
jpluijmers
Thanks for your answer. I implemented your code and by adding a alert before the return false I recognize that the if becomes true. But I think it does not work correctly.I can still call the toggle effect very often by fast clicking on the object.You can see a similar effect at the jquery documentation demo of toggle()http://api.jquery.com/toggle/ See the example with the toggle em button and click it very fast very often. The animations are done even if you don't click anymore. I still have this effect with my code and the click function you posted above.Any help on that?
Phil
@Phil - In that case you're queuing animations (also make sure your selector is checking the right elements...the ones you're animating). If you want `.toggle()` to stop animations when doing the *other* animations, take a look at [`.stop()`](http://api.jquery.com/stop/) for example you'd probably want `$(this).stop(true, true).animate(...)` instead of `$(this).animate()`, whatever element you're animating, just `.stop()` to end the previous animation, the `true` arguments are to skip to the end and clear the queue.
Nick Craver
Well the "animations" are not only animate() functions.I am also doing changes to the style attribute of the element.1. css changes: position absolute, z-index 999 ans positioning stuff2. animate width dan height changes 3. hide of child element of the animated element4. show of other child element while using setTimeoutThe second function in the toggle is more or less bringing the element back to its original state:1. animate width and height2. hide and showof element mentioned in 4./3. + setTimeout too4. removing attr style Several clicks cause chaos primarily with the z-index
Phil
@Phil - Are you animating several elements or a specific one or...? Any check what what you're after is completed or not is all you need, setting any other variable would work here. For example you can just create a `var animating = false;` set it to `true` first thing in the toggle animations, `false` when you're done, in my above code you'd just check `if(animating) return false;`, make sense? or are there other issues with this?
Nick Craver
Well if i do so I have to set animating = true; before the if statement, set it to false and after all animations true again in each of the functions. Well maybe I am doing something wrong or understood you wrong but it does not work at all. Although I understand what we want to do and think it absolutely makes sense... At the moment I have no idea after all..I did: animating = true; if() .. toggle( function animating false ... = true , function animating false .... = true) Just a quick and dirty overview.
Phil
Ah i forgot: Just animating a div with the specific class and the index $(".class").index(this); returns ;)
Phil