我從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循環,如何處理這個完成之前調用。
你不阻止正常的'for'循環,你重寫它異步。 ['async.eachSeries'](https://github.com/caolan/async)是一個很好的起點。 – DCoder 2013-03-09 18:32:15
嗨@DCoder感謝您的回覆,您能否向我建議我的代碼,我將非常感謝您。 – 2013-03-09 18:35:33
嗨@DCoder無法用async.eachSeries編寫代碼,你可以提出一些建議。 – 2013-03-11 18:17:58