jqueryforeach循环中的javascript函数不等待响应

jqueryforeach循环中的javascript函数不等待响应,javascript,jquery,asynchronous,foreach,Javascript,Jquery,Asynchronous,Foreach,我的foreach循环: jQuery(".custom-checkbox").each(function() { if (jQuery(this).attr('data-action') == 'true') { if(deleteQuoteItemFromListing(jQuery(this).attr('data-id'))){ console.log('passed'); }else{

我的foreach循环:

jQuery(".custom-checkbox").each(function() {
      if (jQuery(this).attr('data-action') == 'true') {

      if(deleteQuoteItemFromListing(jQuery(this).attr('data-id'))){
                    console.log('passed');
            }else{
                    console.log('failed');
            }
      }    
 });
功能是使用原型,但它成功了

function deleteQuoteItemFromListing(id){
    //does someoperations and on success
    delurl = getDelUrl()+id; //baseurl/module/action/delete/id
    new Ajax.Request(delurl,{
        method: 'get',
        onSuccess: function(transport){
            return TRUE;
        } 
    })
}
但问题是所有foreach一次执行,并且不等待函数的响应。即使操作成功,它也打印失败

更新

我第一次尝试的另一种方法是

jQuery('.delete-from-quote').click(function() {
        var i = 0, j = 0;
        jQuery(".custom-checkbox").each(function() {
            if (jQuery(this).attr('data-action') == 'true') {
                i++;
            }
        });


        if (i == 0) {
            alert('please choose product');
            return false;
        }

        jQuery(".custom-checkbox").each(function() {

            if (jQuery(this).attr('data-action') == 'true') {

                var urlData = "<?php echo $this->getUrl('qquoteadv/index/delete/'); ?>";
                urlData += "id/" + jQuery(this).attr('data-id') + "/"

                var ajax = jQuery.ajax({
                    type: "GET",
                    url: urlData,
                    success: function(msg) {
                        j++;
                    }
                })

            }
          if(i==j){location.reload();} //after completing all, reload the page
        });
  });

问题是要知道所有操作都已完成并重新加载页面。

我猜您忽略的代码正在执行异步ajax调用。由于默认情况下ajax是异步的,因此您在那里编写的代码$.ajax或其他任何东西都会启动该进程,但该进程会在后台继续运行,而您的代码会继续运行

没有合理的方法使deleteQuoteItemFromListing函数等待响应。虽然可以执行同步ajax,但A它会通过锁定浏览器UI而导致糟糕的用户体验,而B jQuery将在某个阶段删除该选项,如果您想继续这样做,就必须直接转到XHR

相反,通过让您的函数返回承诺或接受回调,然后解析承诺或在完成时调用回调,重新构造代码以接受web编程的异步特性

以下是promise版本的大致情况:

jQuery(".custom-checkbox").each(function() {
    if (jQuery(this).attr('data-action') == 'true') {
        deleteQuoteItemFromListing(jQuery(this).attr('data-id'))
            .done(function(id) {
                console.log(id + ' passed');
            })
            .fail(function(id) {
                console.log(id + ' failed');
            });
    }    
});

function deleteQuoteItemFromListing(id){
    var d = jQuery.Deferred();
    jQuery.ajax(/*...*/)
        .done(function() {      // This bit assumes the deletion worked if
            d.resolveWith(id);  // the ajax call worked, and failed if the
        })                      // ajax call failed; if instead the ajax
        .fail(function() {      // call always works and returns a flag,
            d.rejectWith(id);   // adjust accordingly
        });
    return d.promise();
}

您的部分问题在于以下简单陈述:

return TRUE;
在JavaScript中,真正的布尔值是用小写字母编写的:

return true;

解释器认为TRUE是一个变量,并且会抛出ReferenceError,因为它没有在任何地方设置/定义,这意味着函数永远不会返回TRUE。

使用回调确保函数被执行

jQuery(".custom-checkbox").each(function () {
    if (jQuery(this).attr('data-action') == 'true') {
        deleteQuoteItemFromListing(jQuery(this).attr('data-id'), handleData);

    }
});

    function handleData(data) {
    if (data) {
        console.log('passed');
    } else {
        console.log('failed');
    }
}


function deleteQuoteItemFromListing(id, callback) {
    //does someoperations and on success
    delurl = getDelUrl() + id; //baseurl/module/action/delete/id
    new Ajax.Request(delurl, {
        method: 'get',
        onSuccess: function (transport) {
            callback(true);
        }
    })
}
我希望这对你有用。您需要在其他函数之外定义handleData函数。

在需要时使用jquery

您需要在一个延迟数组中对延迟的函数进行排队,然后一次应用所有函数

如果一个人失败了,所有人都会失败;如果所有人都成功了,所有人都会通过

看看这个


是的,对该函数进行了ajax调用。@K.C:您可能想更新问题来说明这一点,但我实际上猜对了,所以这个答案解决了这个问题。回调函数没有trigger@K.C.我给了你一个想法,它没有必要运行,你可以进一步调试你的代码;
      var queue = [];
      var items = 0;
      return new $.Deferred(function (deferred) {
      $(".custom-checkbox").each(function () {
       if ($(this).attr('data-action') == 'true') {
        items++;
        queue.push(function () {
            new Ajax.Request(delurl, {
                method: 'get',
                onSuccess: function (transport) {
                    items--;
                    if(items === 0)
                        deferred.resolve();
                },
                onError:function(e){
                  deferred.reject(e);   
                }
            });
        });
    }
});

//now resolve all of the deferred fns

  $.when(queue).done(function(){
      console.log('All went well');
  })
 .fail(function(e){
      console.log('Error ' + e);
  });
});