0

我想获取 facebook 的 api 值并返回多个变量,以便我可以在我网站上的几个不同页面上使用它。
这是我想做的,但由于 FB 是async延迟的值。

当它们准备好时,我将如何获得这些值?

function getFBinfo(){
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            FB.Canvas.getPageInfo(
                    function(info) {

                            var myClientHeight = info.clientHeight;
                            var myClientWidth = info.clientWidth;
                            var myOffsetLeft = info.offsetLeft;

                            return {clientHeight:myClientHeight, clientWidth:myClientWidth, offsetLeft:myOffsetLeft };
                    }
            ); 

        }
    });
}

然后访问某个页面上的变量,例如:

en  var n = getFBinfo(); 
console.log(n.clientHeight);
4

1 回答 1

1

由于 facebook getPageInfo 方法是异步方法,所以你可以传递回调而不是返回值,试试这个,

function getFBinfo(callback){
FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        FB.Canvas.getPageInfo(
                function(info) {

                        var myClientHeight = info.clientHeight;
                        var myClientWidth = info.clientWidth;
                        var myOffsetLeft = info.offsetLeft;
                        callback({clientHeight:myClientHeight,     clientWidth:myClientWidth, offsetLeft:myOffsetLeft });
                }
        ); 

    }
  });
}

function mycallback(obj)
{
     //You can handle output here
     console.log(obj.clientHeight);
 }

 var n = getFBinfo(mycallback); 
于 2013-06-11T04:37:25.420 回答