0

IN.API.PeopleSearch()从一个for循环调用,这个for循环在ajax成功方法中,但是在完成for循环执行之前,ajax方法完成被调用。

我想停下来,直到 for 循环完成。

$.ajax({
        type: 'GET',
        dataType: 'json',
        url: "get_data.htm",
        async : false,
        success: function(data, textStatus ){
            for(i in data){
                searchClick(data[i].firstName,data[i].lastName);
                }
                alert(resultArray);//here i want to send the response to server side
            }
        },
        error: function(xhr, textStatus, errorThrown){
           alert('request failed');
        }
      });

这是我的 searchClick 功能:

function searchClick(firstName, secondName) {
  if (!IN.ENV.auth.oauth_token) {
    alert("You must login w/ LinkedIn to use the Search functionality!");
    return;
  }

  IN.API.PeopleSearch()
      .fields("id", "firstName", "lastName","emailAddress","headline","industry","pictureUrl","positions",
            "summary","numConnections")
    .params({
      "first-name": firstName,
      "last-name": secondName
    })
    .result(function(result, metadata) {

    for (i in result.people.values) {
          try{
              resultArray[i] = result.people.values[i];
          }catch(err){
              alert(err);
              }
    }

    });
}

alert(resultArray)在 for 循环完成之前被调用,如何处理。

4

2 回答 2

1

我不知道我是否得到你的问题,但也许这样的东西对你有用:(未经测试)

var Queue = function(callback) {
  this.count = 0;
  this.done = 0;
  this.callback = callback;
};

Queue.prototype.oneDone = function() {
  if (++this.done == this.count) {
    this.callback();
  }
}

Queue.prototype.process = function(data, callback) {
  this.count = data.length;

  for (i in data ) {
    callback(data[i], this);
  }
};

$.ajax({
  type: 'GET',
  dataType: 'json',
  url: "get_data.htm",
  async : false,
  success: function(data, textStatus) {
    var myQueue = new Queue(function() {
      alert(resultArray); //here i want to send the response to server side
    });
    myQueue.process(data, function(item, queue) {
      searchClick(item.firstName, item.lastName, queue);
    });
  },
  error: function(xhr, textStatus, errorThrown){
    alert('request failed');
  }
});

function searchClick(firstName, secondName, queue) {
  if (!IN.ENV.auth.oauth_token) {
    alert("You must login w/ LinkedIn to use the Search functionality!");
    return;
  }

  IN.API.PeopleSearch()
    .fields("id", "firstName", "lastName","emailAddress","headline","industry","pictureUrl","positions",
            "summary","numConnections")
    .params({
      "first-name": firstName,
      "last-name": secondName
    })
    .result(function(result, metadata) {
      for (i in result.people.values) {
        try {
          resultArray[i] = result.people.values[i];
        } catch(err) {
          alert(err);
        }
      }
      if (queue) {
        queue.oneDone();
      }
    });
  }
于 2013-03-11T22:05:44.207 回答
0

我不知道你到底在做什么,但可以说我们有一种方法,异步

    Function.prototype.async = function () {
        setTimeout.bind(null, this, 0).apply(null, arguments);
        };

这使我可以编写如下代码:

   alert.async("This will be displayed later.");
   alert("This will be displayed first.");

因此,一旦其他事件完成,将调用带有 .async 的代码。


否则,在您的情况下,请使用

 if(xmlhttp.readyState == 4 && xmlhttp.status == 200)

检查文档是否准备好然后发送 /fill /success。这是原始 AJAX 方法。:)

O希望这可能会有所帮助:)

于 2013-03-12T05:11:02.007 回答