2012-10-26 129 views
3

有很多类似的帖子,但没有什么比较适合我的需求。所以我被迫创建一个帖子。ajax在IE 8中速度很慢,但在Firefox和Chrome中速度很快

名称列表显示在jsp页面上。当您将鼠标悬停在名称上时,我会进行ajax调用,以在工具提示窗口中显示该名称的详细信息。

用户将使用IE8。在IE8中完成这个简单的事情大概需要大约5秒钟的时间,就像在Firefox和Chrome中它瞬间发生一样。

我还注意到,随着名称显示数量的增加或减少,响应时间也在IE8中也一样。

如何在IE8中使其更快?

jsp页面

<td onmouseover ="showDetails('${origin }')"> 
    <a href="#"><c:out value="${origin}"></c:out><span id="info"></span></a> 
</td> 

javascript函数

function showDetails(stop){ 
    var xmlHttpRequest; 
    if(window.XMLHttpRequest){ 
     xmlHttpRequest = new XMLHttpRequest(); 
    }else{ 
     xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlHttpRequest.onreadystatechange=function(){ 
     if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){ 
      document.getElementById("info").innerHTML = xmlHttpRequest.responseText; 
     } 
    } 
    xmlHttpRequest.open("GET", "showStopsInfoPopup.do?stop="+stop, true); 
    xmlHttpRequest.send(); 
} 
+2

为什么你使用jQuery标签,如果你不使用任何jQuery代码?如果您使用jQuery,只需使用['.load'](http://api.jquery.com/load/)方法。 – Blazemonger

+1

我道歉。我对ajax比较陌生。所以我认为有人会知道jquery也会知道这个问题。当我搜索这个问题时,似乎很多有类似问题的人都在使用jQuery。 – Susie

+1

延迟可能实际上是由于innerHTML,而不是ajax请求。我建议你分开计时。 – Christophe

回答

4

试试这个。

function showDetails(stop){ 
    var t1 = new Date().getTime(); 
    var xmlHttpRequest; 
    if(window.XMLHttpRequest){ 
     xmlHttpRequest = new XMLHttpRequest(); 
    }else{ 
     xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlHttpRequest.onreadystatechange=function(){ 
     if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){ 
      alert("Call took " + (new Date().getTime() - t1) + " milliseconds"); 
      document.getElementById("info").innerHTML = xmlHttpRequest.responseText; 
     } 
    } 
    xmlHttpRequest.open("GET", "showStopsInfoPopup.do?stop="+stop, true); 
    xmlHttpRequest.send(); 
} 

你可能会看到这个调用同样快,但它的后续渲染反应在IE8中很慢。

如果您确认,那么问题是关于responseText中的内容。

+0

我做了上述,我得到一个警告框,其中包含消息“Nan milliseconds” – Susie

+0

在响应文本中,字符串通常少于20个字符。 – Susie

+1

编辑时间计算部分,再试一次。 – stacktrace

1

如果你想使用jquery:

function showDetails(stop) { 
    $('#info').load("showStopsInfoPopup.do?stop=" + stop); 
} 
+0

试过这个。它从总时间减少了约2秒。但它仍然需要3秒左右。 – Susie

2

相反:

xmlHttpRequest.onreadystatechange=function(){ 
    if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){ 
     alert("Call took " + new Date().getTime() - t1 + " milliseconds"); 
     document.getElementById("info").innerHTML = xmlHttpRequest.responseText; 
    } 
} 
xmlHttpRequest.open("GET", "showStopsInfoPopup.do?stop="+stop, true); 
xmlHttpRequest.send(); 

试试这个:

xmlHttpRequest.open("GET", "showStopsInfoPopup.do?stop="+stop, true); 
xmlHttpRequest.onreadystatechange=function(){ 
    if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){ 
     alert("Call took " + new Date().getTime() - t1 + " milliseconds"); 
     document.getElementById("info").innerHTML = xmlHttpRequest.responseText; 
    } 
} 
xmlHttpRequest.send(null); 

修复新日期

在你的代码中缺少括号(记住分开Mathematics,Strings)。尝试:

xmlHttpRequest.open("GET", "showStopsInfoPopup.do?stop="+stop, true); 
xmlHttpRequest.onreadystatechange=function(){ 
    if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){ 
     alert("Call took " + (new Date().getTime() - t1) + " milliseconds"); 
     document.getElementById("info").innerHTML = xmlHttpRequest.responseText; 
    } 
} 
xmlHttpRequest.send(null); 

测试 Ajax请求:

<div id="info"></div> 
<script>var xhr, t1; 
if(window.ActiveXObject){ 
    try { xhr=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ 
     try { xhr=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){} 
    } 
} else if(window.XMLHttpRequest){ 
    xhr=new XMLHttpRequest(); 
} 

xhr.open("GET", "teste.php", true); 

t1 = new Date().getTime();//I start the timer that point to prevent the previous functions affect the end result 

xhr.onreadystatechange=function(){ 
    if(xhr.readyState == 4 && xhr.status == 200){ 
     document.getElementById("info").innerHTML = "Call took " + (new Date().getTime() - t1) + " milliseconds"; 
    } 
} 
xhr.send(null); 
</script> 

PHP(泰斯特。PHP):

<?php 
sleep(5); 
echo 'ok'; 
?> 

结果:

  • IE8:5004毫秒

  • Chorme:5005毫秒

  • 火狐:5014毫秒

  • IE7:5023毫秒

  • IE6:5053毫秒

试验后,得出结论认为,或许还有对服务器端的侧面,更确切地说在你的PHP。

+0

我收到了一个警告框,显示“南毫秒”消息 – Susie

+0

我编辑了答案,看起来好像是 –

+0

这条消息提到“呼叫花费了5696毫秒”。通常约为5000毫秒。 – Susie

相关问题