JS Exception Is Still Able To Sneak In But Very Rarely, Not Sure Why
Solution 1:
This is failing because your timeout is too short. When the request times out, the callback is removed from the window, and then when the request actually finishes, an exception is thrown because the callback isn't on the window. It doesn't happen every time because you're using the same callback name for every request, and sometimes it just so happens to complete while another request hasn't timed out yet resulting in a "success" that wasn't actually a success.
Your code was actually pretty close, the root of the issue here is that you didn't give each request a unique JSONPCallback. You simply need to move those variables into the ajax function so that they will be re-created for each request, resulting in each getting it's own auxTime based callback name.
// JSON Request
var timeout = setTimeout(callAjax, 5000)
function callAjax() {
var auxTime = new Date();
var jQueryCallbackRandom = auxTime.getTime();
var callParameters = {
url: 'http://jsfiddle.net/echo/jsonp/',
timeout: 5,
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;
};
}
};
$.ajax(callParameters);
clearTimeout(timeout);
}
Post a Comment for "JS Exception Is Still Able To Sneak In But Very Rarely, Not Sure Why"