需求:当与服务器断开链接的时候,能给用户以提示。
问题:我写了下面的代码测试连通性,自己做测试的时候,如果客户端断开网络链接,可以正常提示,如果服务端的IIS服务停止网站,也可以正常提示。但是,如果服务器拔掉网线,整个页面就卡死不动了。在chrome浏览器的表现是卡一段时间页面崩溃.求指点。 ////检测与服务器的连接性 hanliang 2011年7月16日14:41:33
function checkConnection()
{
try{
var xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp");
xmlhttp.open("GET","default.aspx",false);
xmlhttp.send();
}
catch(ex)
{
xmlhttp.abort();
Ext.MessageBox.alert("提示","与服务器断开或网络异常.");
}
setTimeout("checkConnection()",10000);
}
checkConnection();
////end
问题:我写了下面的代码测试连通性,自己做测试的时候,如果客户端断开网络链接,可以正常提示,如果服务端的IIS服务停止网站,也可以正常提示。但是,如果服务器拔掉网线,整个页面就卡死不动了。在chrome浏览器的表现是卡一段时间页面崩溃.求指点。 ////检测与服务器的连接性 hanliang 2011年7月16日14:41:33
function checkConnection()
{
try{
var xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp");
xmlhttp.open("GET","default.aspx",false);
xmlhttp.send();
}
catch(ex)
{
xmlhttp.abort();
Ext.MessageBox.alert("提示","与服务器断开或网络异常.");
}
setTimeout("checkConnection()",10000);
}
checkConnection();
////end
如果本域服务器可以用ajax本身支持的TimeOut属性来做
function checkConnection()
{
var xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp");
xmlhttp.open("GET","default.aspx",false);
xmlhttp.onreadystatechange = function()
{
if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 )
{
clearTimeout(clean); // 如果准备状态成功,清除setTimeout
alert("连接成功");
}
};
var clean = setTimeout(function()
{
xmlhttp.abort(); // 终止XMLHttpRequest对象
alert("超时或网络异常,请您刷新页面或稍后再试");
},60000); //如果请求不成功 设定个时间执行该函数
xmlhttp.send(null);
}