【发布时间】:2012-03-18 04:18:51
【问题描述】:
我刚刚注意到我在安装 Firefox 11 后的 web 应用上出现了一些奇怪的行为。我以前没有看到过这个错误,并且该网站已经运行了一年多。
var timeOutTimer = null;
var StartDownload = function () {
xhr.open("GET", "/Download", true); //Notice asynchronous=true
xhr.onreadystatechange = DownloadComplete;
xhr.send("...");
timeOutTimer = new Timer(......); //This line gets executed AFTER DownloadComplete()
};
var DownloadComplete = function () {
if (xhr.readyState == 4) {
timeOutTimer.Abort(); //<--------timeOutTimer is null here
//Callstack points back to xhr.send
}
}
XmlHttpRequest 真的可以在退出 send() 函数之前调用 onreadystatechange-callback 吗?
仅当我在本地开发服务器上浏览网站时才会出现此错误。此外,如果我添加 1 秒延迟服务器端,则没有问题。我没有在任何其他浏览器中尝试过。
我想解决方案是在发送之前启动计时器,但我只想知道这种行为背后的原因以及是否可以,因为我以前从未经历过。
【问题讨论】:
-
您是在重新初始化现有的 XHR 对象,还是在每次调用 StartDownload 时创建一个新对象?您的代码中有任何
alert()调用吗?
标签: javascript firefox xmlhttprequest