2013-03-09 34 views
0

我從for循環中調用IN.API.PeopleSearch(),而這個for循環是在ajax成功方法中,但是在循環執行完成之前,ajax方法complete纔會被調用。如何停止一個方法的執行,直到另一個方法在javascript中使用Linkedin API人員搜索完成?

我想停止,直到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循環,如何處理這個完成之前調用。

+0

你不阻止正常的'for'循環,你重寫它異步。 ['async.eachSeries'](https://github.com/caolan/async)是一個很好的起點。 – DCoder 2013-03-09 18:32:15

+0

嗨@DCoder感謝您的回覆,您能否向我建議我的代碼,我將非常感謝您。 – 2013-03-09 18:35:33

+0

嗨@DCoder無法用async.eachSeries編寫代碼,你可以提出一些建議。 – 2013-03-11 18:17:58

回答

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(); 
     } 
    }); 
    } 
+0

它說隊列沒有被定義爲 在回調(data [i],隊列); – 2013-03-12 15:02:59

+0

對,它應該是「這個」,我會改變它。 – grilix 2013-03-12 15:07:26

+0

非常感謝你... – 2013-03-12 15:08:04

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) 

檢查文檔是否準備好然後發送/填充/成功。這是Raw AJAX方法。 :)

O希望這可以幫助:)

相關問題