Blynn
Blynn

Reputation: 1421

getting jquery ajaxStop to only fire once everytime

For some reason my ajaxStop code is firing twice right now and I need to limit it to just one time.

$('.submit').bind('click', function(e) {

$("#fileupload").ajaxForm({
        target: '#status'
}).submit();


$(document).ajaxStop(function() {
         var mysrc = $("#status").find('img').attr("src");
         $(".empty:first").removeClass("empty").removeClass("ui-state-default").addClass("ui-state").append('<img src="'+mysrc+'" width="45" height="60" class="expand"/>');
     });     

});

Upvotes: 5

Views: 5995

Answers (2)

user736893
user736893

Reputation:

Mine was executing twice as well. However, using .one meant that it only fired on the FIRST iteration, which wasn't after everything completed. Here is what I ended up using, and it seems to work as desired.

var ajaxCount = 0;

$(document).ajaxStop(function () {
    ajaxCount += 1;
    if (ajaxCount == 2) {
        $('.fieldLoading').hide();
        $('.fieldValue').show();
    }
});

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

try using .one(), like:

$(document).one("ajaxStop", function() {
   var mysrc = $("#status").find('img').attr("src");
   $(".empty:first").removeClass("empty").removeClass("ui-state-default").addClass("ui-state").append('<img src="'+mysrc+'" width="45" height="60" class="expand"/>');
});   

Upvotes: 16

Related Questions