Chrome的加载指示器在XMLHttpRequest期间保持旋转

Mar*_*cny 31 javascript ajax google-chrome comet long-polling

我正在编写一个使用Comet/Long Polling来保持网页最新的AJAX网络应用程序,我注意到在Chrome中,它会将页面视为始终加载(标签图标不断旋转).

我认为这对谷歌Chrome + Ajax来说是正常的,因为即使Google Wave也有这种行为.

那么今天我注意到Google Wave不再保持加载图标旋转,任何人都知道他们如何解决这个问题?

这是我的ajax调用代码

var xmlHttpReq = false;
// Mozilla/Safari
if (window.XMLHttpRequest) {
   xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
   xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpReq.open('GET', myURL, true);
xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttpReq.onreadystatechange = function() {
   if (xmlHttpReq.readyState == 4) {
      updatePage(xmlHttpReq.responseText);
   }
}
xmlHttpReq.send(null);
Run Code Online (Sandbox Code Playgroud)

Mar*_*pel 36

我无耻地偷走了Oleg的测试用例并稍微调整了一下以模拟长轮询.

load.html:

<!DOCTYPE html>
<head>
  <title>Demonstration of the jQery.load problem</title>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script>
  jQuery(document).ready(function() {
    $('#main').load("test.php");
  });
  </script>
</head>
<body>
  <div id='main'></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

test.php:

<?php
  sleep(5);
?>
<b>OK!</b>
Run Code Online (Sandbox Code Playgroud)

结果很有趣:在Firefox和Opera中,XMLHTTPRequests期间没有显示加载指示符.Chrome让它旋转...我怀疑Google Wave不再使用长轮询(例如,每隔X秒轮询一次,以节省资源),但我无法测试它,因为我没有帐户.

编辑:我想通了:加载一点延迟,加载test.php后可能会尽可能小,加载指示器在加载后停止load.html:

jQuery(document).ready(function() {
  setTimeout(function () {
    $('#main').load("test.php");
  }, 0);
});
Run Code Online (Sandbox Code Playgroud)

不知何故,正如在另一个答案的评论中证实的,当浏览器控制回到完成页面渲染时,指示器停止旋转.另一个优点是按下不能中止请求Esc.