2013-03-13 35 views
0

我在調用2個單獨的ajax函數時遇到問題。我有一個循環遍歷頁面上的所有複選框元素,然後如果它們被檢查,它會調用一個ajax函數來移動數據庫中的一些數據。然後,在for循環之外,我調用另一個到我的數據庫的ajax函數,並將結果返回給我的ID元素。我必須以不同的方式命名我的httprequests,所以他們不打架,兩個函數都可以工作,但是我的循環之外的那個函數快速並且不會拉取新的/更改的數據。如果我在這個外部循環函數之前放置一個警報,它就會起作用。我也嘗試過使用setTimeout(myFunctio(),3000),但沒有運氣。Javascript,ajax計時問題

這是我的代碼。

function ungroupContact(){ 
    group = document.getElementsByName("moveGroup")[0].value; 
    for(i=0;i<=25;i++){ 
     if(document.getElementById('checkBox'+i)){ 
      if(document.getElementById('checkBox'+i).checked){ 
       var email = document.getElementById('checkBox'+i).value; 
       moveContact(email, group); 
      } 
     } 
    } 
    //alert("hello"); 
    //setTimeout(alert("hello"),12000); 
    groupList1(group); 
} 

這是我第一次發帖,對不起,如果這是不好的,目前正在攻讀計算機科學學位。

感謝您的任何建議和/或幫助

對不起,我應該知道放了AJAX功能。我使用了w3schools的佈局。

function moveContact(email, group){ 
    if (email=="" || group=="") 
     { 
     document.getElementById("sidebar2").innerHTML="Something wrong was entered"; 
     return; 
     } 
    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==1 || xmlhttp1.readyState==2 || xmlhttp1.readyState==3) 
     { 
     document.getElementById("sidebar2").innerHTML="<img src='images/loading.gif' alt='Loading'> "; 
     } 

     if (xmlhttp1.readyState==4 && xmlhttp1.status==200) 
     { 
     document.getElementById("sidebar2").innerHTML=xmlhttp1.responseText; 
     } 
     } 
    xmlhttp1.open("GET","core/moveContact.php?group="+group+"&email="+email,true); 
    xmlhttp1.send(); 
    return; 
} 
+0

這是因爲ajax請求的異步性質,您需要等到所有請求都完成才發送最後一次調用 –

+1

您能分享'moveContact'方法 –

回答

2

您將需要跟蹤當最後moveContact()異步AJAX功能完成後(只有這樣),調用groupList1()的。

由於您尚未透露可能正在完成ajax調用的moveContact()代碼,因此我們無法真正推薦跟蹤它們的具體信息。一個簡單的方法是設置一個待處理的ajax調用的計數器,並在每個成功處理器中調用ajax調用,檢查計數器是否已經達到零。如果是這樣,您可以致電groupList1(group)

假設你添加一個完成回調到moveContact(),你可以做這樣的:

function ungroupContact(){ 
    group = document.getElementsByName("moveGroup")[0].value; 
    var contactsRemaining = 0; 
    for(i=0;i<=25;i++){ 
     if(document.getElementById('checkBox'+i)){ 
      if(document.getElementById('checkBox'+i).checked){ 
       ++contactsRemaining; 
       var email = document.getElementById('checkBox'+i).value; 
       moveContact(email, group, function() { 
        --contactsRemaining; 
        if (contactsRemaining === 0) { 
         groupList1(group); 
        } 
       }); 
      } 
     } 
    } 
} 


function moveContact(email, group, fn){ 
    if (email=="" || group=="") { 
     document.getElementById("sidebar2").innerHTML="Something wrong was entered"; 
     return; 
    } 
    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==1 || xmlhttp1.readyState==2 || xmlhttp1.readyState==3) { 
      document.getElementById("sidebar2").innerHTML="<img src='images/loading.gif' alt='Loading'> "; 
     } 

     if (xmlhttp1.readyState==4 && xmlhttp1.status==200) { 
      document.getElementById("sidebar2").innerHTML=xmlhttp1.responseText; 
      // we are done now, so call the finish callback 
      if (fn) { 
       fn(); 
      } 
     } 
     } 
    xmlhttp1.open("GET","core/moveContact.php?group="+group+"&email="+email,true); 
    xmlhttp1.send(); 
    return; 
} 
+0

但h你知道最後一次moveContact()是否會在一些早期調用之後完成嗎? –

+0

@pawlakppp - 查看我添加到我的答案的代碼。通過「last moveContact()」,我不符合你發送的最後一個。我的意思是,沒有其他待處理的moveContact()請求 - 它們全部完成。 – jfriend00

+0

哦,我明白了,謝謝你的回答。 –

-1

您可以在成功添加的回調函數,並在該函數執行groupList1功能時,所有的方法執行完畢:

var finished = 0; 
moveContact(email, group, function() { 
    if (++finished == 25) { 
     groupList1(group); 
    } 
}); 
+0

這將不起作用,因爲'for'循環中有'if'語句,並且不一定需要25個'moveContact()'調用。 – jfriend00

+0

@ jfriend00對不起我的壞 – jcubic