为什么函数在数组中执行但是在与jquery'when'一起使用时不执行

问题描述 投票:0回答:1

我试图将动态函数传递给'when'语句但是当'when'被注释掉时,函数仍然从数组中调用。

multiAjaxCalls();
function multiAjaxCalls()
{
      var deferParams = [];
      var numOfAjaxToCall = 2; //could be 1 or 2
      if (numOfAjaxToCall === 1) {
            deferParams = [ajax1('1')];
      }
      else if (numOfAjaxToCall === 2) {
            deferParams = [ajax1('1'), ajax1('2')];
      }

      //If this is commented out then the function(s) in the array above still execute
      //If this is NOT commented out, the function only executes once
      $.when.apply($, deferparams).then(
          function () {
            console.log("all ajax calls have been completed, combination of data can happen now.");
            var objects = arguments;
            console.log(objects);
          },
          function (event) {
              console.log("failed in when ", event);
          }
      );

      function ajax1(posnum)
      {
            return ajaxCommon('https://jsonplaceholder.typicode.com' + '/posts/' + posnum);
      }
      function ajax2(posnum)
      {
            return ajaxCommon('https://jsonplaceholder.typicode.com' + '/posts/' + posnum);
      }

      function ajaxCommon(siteURL)
      {
            console.log("starting site url query: ", siteURL);
            return $.ajax({
                  url: siteURL,
                  method: 'GET'
            })
                  .done(function (data)
                  {
                        //console.log("DONE", data);
                        return data;
                  })
                  .fail(function (data)
                  {
                        //console.log("failed INSIDE AJAX URL:", siteURL, "Data: " , data);
                        return data;
                  })
      }
}

我从上面得到的控制台日志发生一次(这是我所期望的): 起始网站网址查询:https://jsonplaceholder.typicode.com/posts/1 起始网站网址查询:https://jsonplaceholder.typicode.com/posts/2

如果我注释掉'when'块以便数组中的所有函数都不再执行,我在控制台中得到相同的输出,这意味着数组中的函数仍然在执行。

为什么使用'when'时数组中的函数执行一次,但当该块被注释掉时仍然执行?另外,如果有更好的方法使用'when'处理动态函数,请告诉我。

谢谢。

javascript jquery arrays .when
1个回答
3
投票

而不是这个:

deferParams = [ajax1('1'), ajax1('2')];

做这个:

deferParams = [() => ajax1('1'), () => ajax1('2')];

在第一个实际上,当您将函数传递给数组时,您实际上正在执行该函数

编辑:

为了完成这项工作,我在你的代码上做了一些重构:

function getPost(postNum) {
  console.log('Calling with', postNum);
  return ajaxCommon('https://jsonplaceholder.typicode.com' + '/posts/' + postNum);
}

function ajaxCommon(siteURL) {
  console.log("starting site url query: ", siteURL);
  return $.ajax({
    url: siteURL,
    method: 'GET'
  });
}

function multiAjaxCalls() {
  var numOfAjaxToCall = 2; //could be 1 or 2

  var posts = [];
  for (var i = 0; i < numOfAjaxToCall; i++) {
    posts.push(i);
  }

  $.when.apply(null, posts.map(p => getPost(p)))
    .then(function () {
      console.log("all ajax calls have been completed, combination of data can happen now.");
      var objects = arguments;
      console.log(objects);
    })
    .fail(function(e) {
        console.log('A call failed', e);
    });
}

要修复它,我不是将已执行函数的数组传递给apply,而是使用map在apply中调用它们。哪个类似的东西,但只有当when实际被调用。

这是一个小提琴:https://jsfiddle.net/bqpu2wdm/2/

© www.soinside.com 2019 - 2024. All rights reserved.