2010-12-04 273 views
0

我新的AJAX處理多個Ajax請求

我面臨的問題是,我有一個數據庫查詢需要2分鐘來執行......而它在執行我希望用戶得到最新的更新即每30秒發生一次從數據庫處理的訂單數量。

在我開始訂單處理查詢之前..我打印訂單staus ..精細..然後我開始訂單處理查詢和之間如果我點擊更新按鈕..它等待訂單處理完成然後給我更新...這裏是我的代碼

<html> 
<head> 
<script type="text/javascript"> 


function getupdate() 
{ 
v=document.getElementById('test').value; 

if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    {alert(v); 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 

    document.getElementById("update").innerHTML=v; 
    //document.getElementById("chunks").innerHTML=xmlhttp.responseText; 

    } 
    } 

xmlhttp.open("POST","querygoogle.php",true); 
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded") 
xmlhttp.send("process=9"); 
} 


function chunks() 
{ 
    if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp1=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp1.onreadystatechange=function() 
    { 
    if (xmlhttp1.readyState==4 && xmlhttp.status==200) 
    { 
     document.getElementById("chunks").innerHTML=xmlhttp1.responseText; 

    } 
    } 
xmlhttp1.open("POST","querygoogle1.php",true); 
xmlhttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded") 
xmlhttp1.send("process=0"); 
//xmlhttp.send(); 
} 
</script> 
</head> 
<body > 
<span id="txtHint"></span><input name="test" type="text" id="test" onchange="getupdate();" value="3"/> 
<p>Status: </p> 
<div id="chunks" > 
    <p>Being Submitted for Analysis</p> 
    <p> 
    <input name="search" type="button" value="search" onclick="chunks();"> 
    </p> 
</div> 
<p>Update: </p> 
<div id="update" ></div> 

</body> 
</html> 

任何人都可以請求幫助。謝謝

+0

如果您的數據庫查詢需要2分鐘,請嘗試優化它們(例如,通過使用索引)。 – ThiefMaster 2010-12-04 12:01:28

回答

1

聽起來像你的問題是服務器端而不是客戶端。如果我的理解正確,您希望流程如下:

  1. 客戶端發送數據庫查詢請求。
  2. 客戶端請求更新。
  3. 已收到更新。
  4. 客戶端請求更新。
  5. 已收到更新。 (重複至...)
  6. 收到查詢結果。

你做onreadystatechange是異步的,所以客戶端界面只是坐在那裏等待Ajax調用從服務器返回的行動 - 如果這沒有發生,直到處理完成,這意味着您的要求在服務器上停滯不前。

您確定請求更新的服務器端會立即返回狀態,還是有可能在處理完成之前停止?

0

是的,這似乎是服務器端問題。看起來服務器無法處理您的更新請求,因爲它正在運行數據庫查詢。