views:

1395

answers:

3

i want to disable the click handler when the toggle animation is showing so the animation won't build up because of multiple fast click. thanks

jquery script:

$("#button").click({
    $(this).unbind('click').next().toggle("slow",function(){$('#button').bind('click')});
});

html code

<div
   <a href='#' id='button'>Toggle</a>
   <div>
    test
   </div>
</div>
A: 

First off, the way you are attaching events is broken. I don't know if that's just haste in asking the question or a real error in your code, but here's the correct way to set your handler:

$("#button").click(function(evt) { // <- you need to pass it a function!
    $(this).unbind('click').next()
       .toggle("slow",function(){$('#button').bind('click')});
});

But onto your problem. I think that you will benefit from using jQuery's one function. As soon as the click handler is called, it is removed as a click handler; therefore, it only executes once, for one click. To place the click handler back on after the animation, I think you got it right; re-bind it after the animation finishes.

function clickHandler(evt) {
    $(this).next().toggle("slow",
        function() {
            $('#button').bind('click', clickHandler);
        });
}

$("#button").one('click', clickHandler);

If using one is somehow too slow for you, the fastest way would be too add the javascript right on the href of the anchor. But you'll need to switch a global boolean variable when you are ready to execute again.

<a href="javascript:clickHandler(); return false;">...</a>

function clickHandler() {
    if( someBoolean ) {
        someBoolean = false;
        //do stuff
    }
    someBoolean = true;
}
geowa4
thanks for the reply. i tried using .one(). however the $('#button') doesn't work anymore on 2nd click and u can still spam the button with fast clicks. i want to achive something like the toggle in here http://www.mozilla.com/en-US/firefox/update/ even if you spam the button with fast clicks the toggle queue doesn't build up. thanks in advance
Greg
did you look up how `one` works? it only works once, hence the name. to make it work for the second click, you have to put it back on. i'll edit to show another solution.
geowa4
revised the clickHandler ->> $('#button').one('click', clickHandler);works now, however the i also need to bind toggle handler for switching the image.is this correct?function toggleHandler(evt) { function(){ $(this).find("img").attr('src','img/left.png'); }, function(){ $(this).find("img").attr('src','img/down.png'); } }Thanks.
Greg
hi, thanks again for the reply, i got it all working now. :D:D:D
Greg
A: 

Rather than ignoring the subsequent clicks, you might get some benefit from the stop() method. See Quick Tip: Prevent Animation Queue Buildup for an example. He's talking about hover, but it holds for anywhere that you're likely to run animations one after the other on the same event.

Alternatively, can you disable the button, rather than drop the whole click handler? It probably expresses the intent of "don't click me again" better than dropping the clicks.

Dan F
A: 

As mentioned your implementation of event attachment is broken. On top of that there is a method in jQuery called .stop which releases you from needing to use bind/unbind.

$("#button").click(function(){
    $(this).next().stop().toggle("slow");
});

All it does is clear the animation queue and stops the current animation letting you launch the next one.

Stop function on jQuery docs. Note that it has two properties - clearQueue and goToEnd. Read more about using this method more robustly.

Dmitri Farkov