Hello,
I had a situation where a server was offline and I took advantage of that to test the ajax timeout and it didn't work. I have a page that does an ajax request against another server which was down at the time. The page basically froze indefinitely. The server has since been returned to operation. Now I simulate an outage by putting a fake hosts file entry and sending the request to an unassigned IP. I have tried numerous methods I've found online and the closest I can find is to use the beforeSend function to set a timer and then refresh the page if the timer runs out. There has to be a better way to do this because that nukes the process instead of an orderly exit. I've also tried setting a timer outside of the ajax function and I can't get that to work. Seems like timeout is broken. Any help or links would be appreciated. Code below:
var messageNum = 0;
function emailResend(fromAddrIn, toAddrIn, serverIDin, recordIDin)
{
$(":button[value='Send']").attr("disabled",true).attr("value","Wait");
var buttonID = ":button[id='"+serverIDin+recordIDin+"']";
$(buttonID).attr("value","Sending...");
var ajaxTimeout;
$.ajax({
cache: false,
dataType: "text",
data: { 'fromAddr': fromAddrIn, 'toAddr': toAddrIn, 'recordServer': serverIDin, 'recordID': recordIDin },
timeout: 10000,
type: "POST",
url: 'emailResend.asp',
beforeSend: function() {
ajaxTimeout = setTimeout(function() {
alert('Unable to resend. Please try again later.');
//XMLHttpRequest.abort();
//ajax.abort();
//xhr.abort();
window.location.reload();
}, 10000);
},
success: function(data,status){
messageNum++;
var sent = false;
var dc = "#FFAAAA";
if(ajaxTimeout) {
clearTimeout(ajaxTimeout);
}
if($("#resendStatusMessage").length < 1)
{
$("body").append("<div id='resendStatusMessage' style='text-align:center;vertical-align:middle;display:none'></div>");
}
if(status == "success")
{
if(data == "0")
{
//Success
$("#resendStatusMessage").attr("title","Success");
$("#resendStatusMessage").html("<br/>Message Resent");
dc = "#AAFFAA";
sent = true;
}
else
{
//emailResend.asp said there was an error
$("#resendStatusMessage").attr("title","Error");
$("#resendStatusMessage").html("<br/>Server Error");
}
}
else
{
//request error
$("#resendStatusMessage").attr("title","Error");
switch(status)
{
case "timeout":
$("#resendStatusMessage").html("<br/>Request timed out.");
break;
case "parsererror":
$("#resendStatusMessage").html("<br/>Parse error.");
break;
case "error":
$("#resendStatusMessage").html("<br/>Unknown error.");
break;
case "notmodified":
$("#resendStatusMessage").html("<br/>Unknown error.");
break;
default:
$("#resendStatusMessage").html("<br/>Unknown error.");
break;
}
}
$("#resendStatusMessage").css("background",dc).dialog({height:50,width:300}).dialog("open");
setTimeout('closeStatusMessage(' + messageNum + ')',5000);
$(":button[value='Wait']").attr("disabled",false).attr("value","Send");
if(sent)
{
//if using jQuery
var buttonID = ":button[id='"+serverIDin+recordIDin+"']";
$(buttonID).attr("disabled",true).attr("value","Sent");
//if using javascript (no worky)
//this.setAttribute('disabled',true);
//this.setAttribute('value','Sent');
}
},
error: function(XMLHttpRequest, status){
//request error
$("#resendStatusMessage").attr("title","Error");
switch(status)
{
case "timeout":
$("#resendStatusMessage").html("<br/>Request timed out.");
break;
case "parsererror":
$("#resendStatusMessage").html("<br/>Parse error.");
break;
case "error":
$("#resendStatusMessage").html("<br/>Error.");
break;
case "notmodified":
$("#resendStatusMessage").html("<br/>Not Modified error.");
break;
default:
$("#resendStatusMessage").html("<br/>Unknown error.");
break;
}
}
});
}
Thanks in advance,
Cliff