facebook - How to wait for javascript function response? - Stack Overflow

I'm making webpage using facebook API.I want to show user's friends in webpage.So I code li

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
Add a ment  | 

1 Answer 1

Reset to default 5

Pass 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信