Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript、ajax定时问题_Javascript_Ajax - Fatal编程技术网

Javascript、ajax定时问题

Javascript、ajax定时问题,javascript,ajax,Javascript,Ajax,我在调用两个单独的ajax函数时遇到问题。我有一个循环,循环页面上的所有复选框元素,如果它们被选中,它将调用一个ajax函数来移动数据库中的一些数据。然后在for循环之外,我调用另一个ajax函数,该函数进入我的数据库并将结果拉回到我的ID元素。我不得不给我的HttpRequest取不同的名字,这样它们就不会发生冲突。这两个函数都可以工作,但我循环之外的函数速度很快,不会提取新的/更改的数据。如果我在这个外部循环函数之前放置一个警报,它就会工作。我还尝试过使用setTimeout(myFunct

我在调用两个单独的ajax函数时遇到问题。我有一个循环,循环页面上的所有复选框元素,如果它们被选中,它将调用一个ajax函数来移动数据库中的一些数据。然后在for循环之外,我调用另一个ajax函数,该函数进入我的数据库并将结果拉回到我的ID元素。我不得不给我的HttpRequest取不同的名字,这样它们就不会发生冲突。这两个函数都可以工作,但我循环之外的函数速度很快,不会提取新的/更改的数据。如果我在这个外部循环函数之前放置一个警报,它就会工作。我还尝试过使用setTimeout(myFunctio(),3000),但运气不佳

这是我的密码

    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);
}
函数ungroup contact(){
group=document.getElementsByName(“移动组”)[0]。值;

对于(i=0;i您需要跟踪上一个
moveContact()
异步ajax函数何时完成,然后(并且仅在那时)调用
groupList1()

由于您尚未公开可能正在进行ajax调用的
moveContact()
代码,因此我们不能真正推荐跟踪它们的细节。一种简单的技术是为
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;
}
函数ungroup contact(){
group=document.getElementsByName(“移动组”)[0]。值;
var触点保持=0;

对于(i=0;i您可以在成功时添加回调函数,并在所有方法执行完毕后在该函数中执行
groupList1
函数:

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

这是因为ajax请求的异步性质,您需要等到所有请求完成后才能发送最后一个调用。您可以共享
moveContact
方法吗?但是,您如何知道在之前的一些调用之后是否会执行最后一个moveContact()。@pawlakppp-请参阅我在回答中添加的代码,我没有遇到您上次发送的请求。我的意思是没有其他挂起的
moveContact()
请求-它们都已完成。@pawlakppp-现在您已经包含
moveContact()
code,我向您演示了如何向其添加完成回调函数。我不是OP,只是在主题中介绍;)嘿,这里是jfriend00,我只是想知道funtion()是做什么的,fn变量是如何工作的,如果这是一个愚蠢的问题,很抱歉。非常感谢您的快速回答。这不会起作用,因为
for
循环中有
if
语句,并且不一定有25个
moveContact()
调用。
var finished = 0;
moveContact(email, group, function() {
   if (++finished == 25) {
       groupList1(group);
   }
});