繁体   English   中英

Facebook API回调响应

[英]Facebook API callback on response

我的Facebook API有问题。 我可能不是最好的方法,但这就是我所得到的。

我有一个request_ids数组,我需要将其转换为用户ID。 我有一个while循环,它查询请求ID并获取用户ID。

function (response) {
    var requestsToSend = new Array();
    var i = 0;

    while (i < response.request_ids.length)
    {
        FB.api('/'+response.request_ids[i], function(res){
            requestsToSend[i] = res['to']['id'];
        });
        i++;
    }
    console.log(requestsToSend[0], requestsToSend[1], requestsToSend[2], requestsToSend[3]);
}

这很好。 但是,当我回显返回的ID(console.log)时,它们是未定义的,因为FB.api尚未返回值/响应。

仅在FB.api返回值之后,有什么方法可以触发console.log吗? 我真的不想设置一个计时器来触发该功能。

将console.log调用放入响应函数中:

function (response) {
    var requestsToSend = new Array();
    var i = 0;
    while(i < response.request_ids.length) {
        FB.api('/'+response.request_ids[i], function(res){
            requestsToSend[i] = res['to']['id'];
            console.log(requestsToSend[i]);
        });
        i++; 
    }       
}

IV解决了问题,但对解决方案不满意

    function (response) {

            var requestsToSend = new Array();
            var i = 0;
            var arry = new Array();


                        FB.api('/'+response.request_ids[0], function(res){
                            if(typeof res['to'] != 'undefined'){
                                useResults(res['to']['id']);
                            }
                        });

                        FB.api('/'+response.request_ids[1], function(res){
                            if(typeof res['to'] != 'undefined'){
                                useResults(res['to']['id']);
                            }
                        });

                        FB.api('/'+response.request_ids[2], function(res){
                            if(typeof res['to'] != 'undefined'){
                                useResults(res['to']['id']);
                            }
                        });

                        FB.api('/'+response.request_ids[3], function(res){
                            if(typeof res['to'] != 'undefined'){                
                                useResults(res['to']['id']);
                            }
                        });

                function useResults(value){
                    arry.push(value);
                    timeToFire(arry);
                }

                function timeToFire(arry){

                    if(arry.length == response.request_ids.length){
                        //console.log(arry);
                        playTheGame(arry[0], arry[1], arry[2], arry[3]);
                    }
                }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM