0

how can i disable click event before fadein and enable it after fadeout?

my js code:

$('a').click(function(){
    // disable click
    $('div').fadeOut(400,
        function(){
            $(this)
                .css('background','yellow')
            .fadeIn(1800, function(){}
             // enable click
             );});
});

i coulda use stop() but i need disable/enable plz modify my http://jsfiddle.net/WBWrM/1/ thx

test
  • 990
  • 8
  • 20
  • 36

2 Answers2

5

Add as the first line of your click callback:

if ($('div').is(':animated')) return;
Mitya
  • 30,438
  • 7
  • 46
  • 79
  • 1
    +1, this works great. I think it's a better idea then what I had suggested. – Rocket Hazmat Jul 06 '12 at 21:43
  • Rocket - but your suggestion works perfect on my code this one keep doing fadein/out when i click multiple times – test Jul 06 '12 at 21:55
  • @questionMonster - eh? It shouldn't, didn't for me when I tested in the Fiddle. Unless I'm missing something, there's really no difference in concept between what my and Rocket's solutions provide. Each just ignores any click that happens while the anim is rolling. – Mitya Jul 06 '12 at 21:57
  • yea your answer is good it works perfect in single link, but when it multiple links fade doesnt stop after clicking few links fast. thanks tough! – test Jul 06 '12 at 22:08
  • That's because your selector (`div`) is far too loose. I used the code you provided, not changing selectors. `div` should be replaced with a selector RELATIVE to the link clicked. What that is, depends on your code. Perhaps it's a sibling, for example. – Mitya Jul 06 '12 at 23:15
3

Add a class to the element, and don't run the event when that class is there.

$('a').click(function () {
    var $this = $(this);
    if ($this.hasClass('noClick')) return;
    // disable click
    $this.addClass('noClick');
    $('div').fadeOut(400, function () {
        $(this).css('background', 'yellow').fadeIn(1800, function () {
            // enable click
            $this.removeClass('noClick');
        });
    });
});
Rocket Hazmat
  • 204,503
  • 39
  • 283
  • 323