rooz rooz - 7 months ago 11
Javascript Question

jQuery: Prevent multiple clicks?

I'm trying to prevent mutiple clicks on a link and I need to wait until the function's complete before allowing another click on the same link.

However, everything I do, the multiple clicks is always allowed.

This is my code:

var active = false;

$('#rotRight').live('click', function(){
if (active) {
return;
}

$(this).attr('id', 'rotRight1');
var curAngle = parseInt($(".selected").getRotateAngle()) || 0;


if($(".selected").rotate({
angle: curAngle,
animateTo: curAngle - 90

})){
active = true;
}
$(this).attr('id', 'rotRight');
active = false;
});


I know I'm in the right path. I just need someone to let me know what i'm missing or if I'm doing something wrong please.

Any help would be appreciated.

Thanks

Answer

The rotate plugin has a callback where you can reset the active flag value

var active = false;

$('#rotRight').live('click', function() {
  if (active) {
    return;
  }

  var curAngle = parseInt($(".selected").getRotateAngle()) || 0;

  active = true;
  $(".selected").rotate({
    angle: curAngle,
    animateTo: curAngle - 90,
    callback: function() {
      active = false;
    }
  })
});
Comments