0

I have a function, there's 2 button after click pass different parameter and execute the same function, click button do ajax load html cover already loaded.

The problem is after I click a then click b then I click .reload_btn it will execute twice.
Is it possible cancel the earlier click a execute function c?

$(".a").click(function(){
    function c(1);
});

$(".b").click(function(){
    function c(2);
});

function c(select_el){
    $('.reload_button').click(function(){
         .. // ajax reload content
    });

};

4 Answers4

0

unbind the event before your click event. $('.reload_button').unbind();

FosterZ
  • 3,863
  • 6
  • 38
  • 62
0

Use .on() and .off() event handler.

$(".a").on('click',function(){
    function c(1);
});

$(".b").on('click',function(){
    function c(2);
});

function c(select_el){
    $('.reload_button').on('click',function(){
            $(document).off('click','.a');
         .. // ajax reload content
    });
};
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • Thanks for reply! this works too but use on and off means when I do `off` should point which selector `on` click before? –  Jul 19 '13 at 07:51
  • 1
    @user1575921 Yes, but can also be used as a replacement of unbind()[depreciated]. instead use .on() and .off() attach/detach an event handler function for one or more events to the selected elements. – Praveen Jul 19 '13 at 08:38
  • @user1575921 I would like you to take a look at this question. http://stackoverflow.com/questions/11784257/jquery-unbind-and-then-bind – Praveen Jul 19 '13 at 08:39
0

Use have to use .abort() operation in your ajax call,

Follow this link REFER

Community
  • 1
  • 1
RONE
  • 5,415
  • 9
  • 42
  • 71
0

You can adapt this code :

var xhr = $.ajax({
    // Your ajax parameters
});

// Kill the ajax request
xhr.abort()

And have a look to this page too.

Community
  • 1
  • 1
Lucas Willems
  • 6,673
  • 4
  • 28
  • 45