I'm making webpage using facebook API.
I want to show user's friends in webpage.
So I code like this
function showFriends(){
var result = getFriends();
for(var i=0; i<result.length; i++){
//show friends in webpage
}
}
function getFriends(){
FB.api( {
method: 'fql.query',
query: 'SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())'},
function(response) {
return response;
}
);
}
Problem is this : getFriends function need a little time, so for loop processes before getting response of getFriends. And I don't want to locate for loop in getFriends function because getFriends function will be used in many other function.
So is there any solution to wait function's response?
I'm making webpage using facebook API.
I want to show user's friends in webpage.
So I code like this
function showFriends(){
var result = getFriends();
for(var i=0; i<result.length; i++){
//show friends in webpage
}
}
function getFriends(){
FB.api( {
method: 'fql.query',
query: 'SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())'},
function(response) {
return response;
}
);
}
Problem is this : getFriends function need a little time, so for loop processes before getting response of getFriends. And I don't want to locate for loop in getFriends function because getFriends function will be used in many other function.
So is there any solution to wait function's response?
Share Improve this question asked Aug 22, 2012 at 2:08 GFbahamutGFbahamut 331 silver badge3 bronze badges 3- Put the loop in the callback perhaps. – Draculater Commented Aug 22, 2012 at 2:12
- Why not calling a function that has the loop from the callback function? – Sednus Commented Aug 22, 2012 at 2:12
- 3 It sounds like what you need is a callback. recurial./programming/… – Lucas Green Commented Aug 22, 2012 at 2:13
1 Answer
Reset to default 5Pass the showFriends
function as a callback.
function showFriends(result){
for(var i=0; i<result.length; i++){
//show friends in webpage
}
}
function getFriends(callback){
FB.api({
method: 'fql.query',
query: 'SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())'
},
callback
);
}
getFriends(showFriends);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742323181a4422226.html
评论列表(0条)