更新:
这是一个如何处理超时的示例:
var xmlHttp = new XMLHttpRequest();xmlHttp.open("GET", "http://www.example.com", true);xmlHttp.onreadystatechange=function(){ if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { clearTimeout(xmlHttpTimeout); alert(xmlHttp.responseText); }}// Now that we're ready to handle the response, we can make the requestxmlHttp.send("");// Timeout to abort in 5 secondsvar xmlHttpTimeout=setTimeout(ajaxTimeout,5000);function ajaxTimeout(){ xmlHttp.abort(); alert("Request timed out");}在IE8中,您可以向对象添加超时事件处理程序
XMLHttpRequest。
var xmlHttp = new XMLHttpRequest();xmlHttp.ontimeout = function(){ alert("request timed out");}我建议不要像代码所暗示的那样进行同步调用,也建议使用javascript框架来做到这一点。 jQuery
是最受欢迎的一种。它使您的代码更高效,更易于维护并且跨浏览器兼容。



