我刚刚注意到安装Firefox 11后,我在webapp上发现了一些奇怪的行为。我之前没有看到过这个错误,并且该站点已经运行了一年多。发送函数完成之前是否可以回调完成的XmlHttpRequest触发器?
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的回调 - 功能?
只有当我在本地开发服务器上浏览网站时才会出现此错误。另外,如果我添加1sec延迟serverside没有问题。我还没有尝试过任何其他浏览器。
我想解决方案是在发送之前启动定时器,但我只想知道这种行为背后的原因,以及它是否正确,因为我从来没有经历过它。
您是否重新初始化现有的XHR对象,或者在每次调用StartDownload时创建一个新对象?你的代码中有没有'alert()'调用? – 2012-03-19 02:55:06