【问题标题】:Timeout XMLHttpRequest超时 XMLHttpRequest
【发布时间】:2010-12-04 04:00:06
【问题描述】:

如何为以下脚本添加超时?我希望它显示文本为“超时”

var bustcachevar = 1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects = ""
var rootdomain = "http://" + window.location.hostname
var bustcacheparameter = ""

function ajaxpage(url, containerid) {
    var page_request = false
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
        page_request = new XMLHttpRequest()
    else if (window.ActiveXObject) { // if IE
        try {
            page_request = new ActiveXObject("Msxml2.XMLHTTP")
        } catch (e) {
            try {
                page_request = new ActiveXObject("Microsoft.XMLHTTP")
            } catch (e) {}
        }
    } else
        return false
    document.getElementById(containerid).innerHTML = '<img src="load.gif" border="0"><br><br><strong>Generating Link...</strong>'
    page_request.onreadystatechange = function () {
        loadpage(page_request, containerid)
    }
    if (bustcachevar) //if bust caching of external page
        bustcacheparameter = (url.indexOf("?") != -1) ? "&" + new Date().getTime() : "?" + new Date().getTime()
    page_request.open('GET', url + bustcacheparameter, true)
    page_request.send(null)
}


function loadpage(page_request, containerid) {
    if (page_request.readyState == 4 && (page_request.status == 200 || window.location.href.indexOf("http") == -1))
        document.getElementById(containerid).innerHTML = page_request.responseText
    else if (page_request.readyState == 4 && (page_request.status == 404 || window.location.href.indexOf("http") == -1))
        document.getElementById(containerid).innerHTML = '<strong>Unable to load link</strong><br>Please try again in a few moments'
}

【问题讨论】:

标签: timeout xmlhttprequest


【解决方案1】:

以 XMLHttpRequest 对象的超时属性为例。

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function () {
    if (xhr.readyState == 4) {
        alert("ready state = 4");
    }
};

xhr.open("POST", "http://www.service.org/myService.svc/Method", true);
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
xhr.timeout = 4000; // Set timeout to 4 seconds (4000 milliseconds)
xhr.ontimeout = function () { alert("Timed out!!!"); }
xhr.send(json);

上面的代码对我有用!

干杯

【讨论】:

  • 编辑代码:超时处理程序应设置为ontimeout,而不仅仅是timeout。 (在搜索相同内容时发现此帖子,MSDN 确认)。
  • @erikvold timeout/ontimeout 在 Firefox 中也受支持,从 Firefox 12.0 开始:developer.mozilla.org/en/xmlhttprequest#Browser_compatibility
  • 提醒其他任何人看到 XHR 和 IE10 出现间歇性错误 - xhr.timeout 值必须在 xhr.open 语句之后设置。如果在我看到IE10间歇性抛出错误之前设置了超时值。
  • 请注意 xhr.timeout 和 setTimeout 之间的区别。前者是请求在被终止之前需要多长时间。后者是您希望进程在执行特定功能之前等待多长时间。
  • 请注意,ontimeout 事件在触发 AFTER readyState == 4 之前不会被触发。这不仅不直观,而且很难根据上传是否由于超时而没有成功来创建条件行为。例如,自定义关于上传失败原因的用户警报。在我的情况下,我发现我只能用 setTimeout 完成我想要的。此外,截至 2016 年,xhr.timeout 似乎不适用于 Safari
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 1970-01-01
  • 2012-03-19
  • 2011-04-25
  • 2015-10-28
  • 1970-01-01
相关资源
最近更新 更多