Javascript 仅在上一次交互完成后的增量(回调)

Javascript 仅在上一次交互完成后的增量(回调),javascript,jquery,callback,Javascript,Jquery,Callback,javascript中的回调函数有问题。我想做的是:在for上循环并调用一个函数,将I作为参数传递。考虑到这一点,只有在上一次交互完成后,我才能循环到下一次交互。我不知道这是否是一个问题,但在作为参数发送的函数I中,我有另一个回调函数。这是我的密码: for(i=0; i<10; i++) { aux(i, function(success) { /* * this should be made interaction by interacti

javascript中的回调函数有问题。我想做的是:在for上循环并调用一个函数,将
I
作为参数传递。考虑到这一点,只有在上一次交互完成后,我才能循环到下一次交互。我不知道这是否是一个问题,但在作为参数发送的函数
I
中,我有另一个回调函数。这是我的密码:

for(i=0; i<10; i++) {
    aux(i, function(success) {
        /* 
         *  this should be made interaction by interaction
         *  but what happens is: while I'm still running my first interaction  
         *  (i=0), the code loops for i=1, i=2, etc. before the response of 
         *  the previous interaction
         */
        if(!success)
            doSomething();
        else
            doSomethingElse();
    });
}

function aux(i, success) {
    ... //here I make my logic with "i" sent as parameter
    getReturnFromAjax(function(response) {
        if(response)
            return success(true);
        else
            return success(false);
    });
});

function getReturnFromAjax(callback) {
    ...
    $.ajax({ 
        url: myUrl,
        type: "POST",
        success: function (response) {
        return callback(response);
    }
});
}

for(i=0;i我没有使用jQuery的经验,但我觉得您的回调有点可疑

在plain JS中,我建议尝试以下几行:

function yourMainFunction
{   

    function callbackHandler(result) 
    {
        // Code that depends on on the result of the callback
    }

    getAjaxResults(callbackHandler);

}



function getAjaxResults(callbackHandler)
{   

// Create xmlHttpRequest Handler, etc.
// Make your AJAX request

xmlHttp.onreadystatechange = function() 
{
    if (xmlHttp.readyState == 4 && xmlHttp.status==200)
    {
        // Do stuff you want to do if the request was successful
        // Define a variable with the value(s) you want to return to the main function

        callbackHandler(yourReturnVariable);
    }
}
}

我建议您研究jQuery的延迟对象,而不是创建自己的回调队列函数(因为您已经在使用jQuery)

描述:返回可链接实用程序的构造函数 对象,该对象具有将多个回调注册到回调中的方法 队列,调用回调队列,并中继成功或失败状态 指任何同步或异步功能

这可能有点棘手。你需要做的是将你的承诺堆叠在一个链中。例如:

var
  // create a deferred object
  dfd = $.Deferred(),

  // get the promise
  promise = dfd.promise(),

  // the loop variable
  i
;

for(i = 0; i < 10; i += 1) {
  // use `then` and use the new promise for next itteration
  promise = promise.then(
    // prepare the function to be called, but don't execute it!
    // (see docs for .bind)
    aux.bind(null, i, function(success) {
      success ? doSomethingElse() : doSomething();
    })
  );
}

// resolve the deferred object
dfd.resolve();
getReturnFromAjax

function aux(i, callback) {
  console.log('executing for `aux` with', i);

  // return the ajax-promise
  return getReturnFromAjax(function(response) {
    callback(Boolean(response));
  });
}
function getReturnFromAjax(callback) {
  // return the ajax-promise
  return $.ajax({
    url: '%your-url%',
    type: '%method%',
    success: function (response) {
      callback(response);
    }
  });
}
演示: