[Solved] How can I detect the last ajax call in a loop?


I assume a typo here, where e should be i

var isLastElement = e == diff.length -1;

But anyway, don’t use the loop index. You will save yourself a lot trouble if you use a variable declared outside of the loop… And increment the value of this variable in the ajax callbacks.

var callCount = 0

for ( var i = 0; i < diff.length ; i++ ) {

     $.ajax({
        method:'POST',
        data: {
        },
        url:'somepath',
        success : function (data) {
        
            if (callCount==diff.length-1) {
              alert('last item');
            }
          
            callCount++
        }
      });
 }

Notice that the requests are asynchonous… And it is not garanteed that the responses will be received in order. So the “last” will be the one that took the longuest time…

0

solved How can I detect the last ajax call in a loop?