如何停止一个方法执行,直到另一个方法在使用Linkedin API人员搜索的javascript中完成?

时间:2013-03-09 18:27:35

标签: javascript linkedin linkedin-jsapi

我从for循环调用IN.API.PeopleSearch(),这个for循环是在ajax success方法中,但在完成循环执行之前,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);
              }
    }

    });
}
在完成for循环之前,

alert(resultArray)被调用,如何处理它。

2 个答案:

答案 0 :(得分: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();
      }
    });
  }

答案 1 :(得分:0)

我不知道你在做什么,但可以说我们有一个方法,async

    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)

用于检查文档是否准备就绪,然后发送/填充/成功。这是Raw AJAX方法。 :)

希望这可能有所帮助:)