With a call limit, as suggested by Hadas:
function ajaxCall(count) { count = typeof count == 'undefined' ? 0 : count; limit = 5; if(count === limit) return; // no need to specify cache and type: // type has 'GET' as default value // cache has default value of false if the dataType is jsonp $.ajax({ url: 'http://server/test.php', dataType: 'jsonp', timeout: 32000, async: false }).done(function(data) { // some actions here }).fail(function(jqXHR, textStatus, errorThrown) { count += 1; console.log("Error[refresh]: " + textStatus); console.log(jqXHR); // 500, 1000, 1500, 2000 etc setTimeout(function() { ajaxCall(count); }, 500*count); }); };
source share