I\'m having a problem when using the jquery .each() and .ajax() functions together. I\'m using .each() to loop through 5 elements and am performing the .ajax() call for each
i'm happy to say there's a way... but it's a bit nasty.
If you are sure the request has to give something back, then you can remove the "tryIteration" part.
$(document).ready(function() {
loop(".buttonsPromotion", 0);
});
function loop(elements, tryIteration) {
//HOW MANY TRIES UNTIL WE STOP CHECKING
if(tryIteration<7){
$(elements).each(function() {
var actuel = $(this);
$.ajax({
url: "write your link here",
data: {},
dataType: 'json',
async: true,
success: function(data){
//HERE WE KNOW THE AJAX REQUEST WENT OK
actuel.addClass('AjaxOK');
},
error:function(thrownError){
console.log(thrownError);
//WE MAKE ANOTHER EACH ON ALL ELEMENT THAT WENT BAD
var elemToRetry = actuel.not('AjaxOK');
loop(elemToRetry, ++tryIteration);
}
});
});
}
}