Question

I haven't been able to solve my problem searching through Stack. I have my live() function below. You can see how I want to stop the button being clicked a second time while the function is running, and then rebind. The button dies unbind, but it doesn't bind again... :S

$('.control-left, .prevActive').live('click',function(){

    var firstSlide = $('.slider li:first-child');

    if(firstSlide.attr('class') != 'active'){   
        $('.control-left, .prevActive').die('click');   
        moveSlider('left');
        $('.control-left, .prevActive').live('click');
    }

});
Was it helpful?

Solution

No need to unbind and rebind just use a flag:

var running = false;
$('.control-left, .prevActive').live('click',function(){
    var firstSlide = $('.slider li:first-child');

    if(firstSlide.attr('class') != 'active' && !running){ 
        running = true;
        moveSlider('left');
        running = false;
    }
});

OTHER TIPS

You are not passing any function to .live.. try this:

$('.control-left, .prevActive').live('click',function hello(){

    var firstSlide = $('.slider li:first-child');

    if(firstSlide.attr('class') != 'active'){   
        $('.control-left, .prevActive').die('click');   
        moveSlider('left');
        $('.control-left, .prevActive').live('click', hello);
    }

});

Your last line of code does not bind anything to the click handler. You must bind a function

$('.control-left, .prevActive').live('click', function () { code here });

In your situation you would probably want to do something like this

var myspecialclickfunction = function(){

  var firstSlide = $('.slider li:first-child');

  if(firstSlide.attr('class') != 'active'){   
      $('.control-left, .prevActive').die('click');   
      moveSlider('left');
      $('.control-left, .prevActive').live('click', myspecialclickfunction);
};

$(document).ready(function () {
  $('.control-left, .prevActive').live('click',myspecialclickfunction);
});

Also jQuery 1.42 to 1.6x you should be using

in jquery 1.7+

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top