e-zero
e-zero

Reputation: 355

Remove event handler used by .ajaxStop

If I attach an event handler using .ajaxStop like this:

$(document).ajaxStop(function() {
    //Some code
});

How do I remove that event handler?

Upvotes: 8

Views: 5553

Answers (3)

XaviQV
XaviQV

Reputation: 185

If you need to ensure that the handler code inside ajaxStop() method is executed before it is removed, I propose the following:

$(document).ajaxStop(function(){    
    // Functions there

    $(this).unbind("ajaxStop");
});

Particulary useful if you have various possible $.ajax in the same page and you want to control where to execute the ajaxStop() code. Then you just have to use this code inside the needed jQuery function.

Upvotes: 0

Matt Finucane
Matt Finucane

Reputation: 181

Try unbinding it, like so:

$(document).ajaxStop(function () {
    $(this).unbind("ajaxStop");
    //Some code
});

It's how I've been doing it.

Upvotes: 17

bipen
bipen

Reputation: 36531

use .off()

Remove an event handler.

$("#elementID").off()

this removes all event handler from the selected element

Upvotes: 3

Related Questions