I wrapped the function with setTimeout it will run forever. If you notice on the screenshot, js exception happened after so many times. That is what is happening on our real app too. I am not sure why.
Here is the fiddle: http://jsfiddle.net/ux12xoya/1/
// JSON Request
var auxTime = new Date();
var jQueryCallbackRandom = auxTime.getTime();
var callParameters = {
url: 'http://jsfiddle.net/echo/jsonp/',
timeout: 2,
dataType: "jsonp",
data: { echo: "Hello World!" },
jsonpCallback: "jQueryRandom_" + jQueryCallbackRandom,
success: function(){
console.log("success");
},
error: function(jqXHR, textStatus){
console.log("failed with error: " + textStatus);
window["jQueryRandom_" + jQueryCallbackRandom] = function() {
window["jQueryRandom_" + jQueryCallbackRandom] = null;
};
}
};
var timeout = setTimeout(callAjax, 5000)
function callAjax() {
$.ajax(callParameters);
clearTimeout(timeout);
timeout = setTimeout(callAjax, 5000)
}
Here is the screenshot of the error: https://i.stack.imgur.com/fQ9hI.png
callAjax
... to makes them locals tocallAjax
scopejQueryCallbackRandom
with every request. If any one request takes longer than 5 seconds, you'll start running into conflicts. Stop manually naming the callback and that will stop happening.callParameters
are just initialized once, so they'll be exactly the same on each$.ajax()
invocation. If you move all that stuff (exception the start-off call tosetTimeout()
inside thecallAjax()
function as @Hacketo suggested, then it will not have those failures. (It will still have timeout errors, because you've set a very short timeout limit.)