编程语言
首页 > 编程语言> > Javascript – 如何等待Facebook

Javascript – 如何等待Facebook

作者:互联网

我的应用执行以下操作:
1.询问friends_online_presence权限.
2.如果用户授予了权限(即回调表示成功),我会立即测试权限(*),并根据结果提取用户的详细信息(调用getOnlineFriends).

问题是facebook需要几毫秒来更新其服务器上的权限.因此,当我立即查询权限时,我刚刚要求facebook总是说我没有权限.
使用setTimout“解决”问题.
如何解决这个问题呢?
我尝试了一些原始形式的轮询,但它让浏览器卡住了(它似乎也是一个弯曲的解决方案).

(*)’SELECT friends_online_presence FROM permissions WHERE uid = me()’

代码:

c2p.facebook = {
    requestViewOnlineFriendsPerms: function (callback) {
        myStuff.requestPermission('friends_online_presence', callback);
    },

    requestPermission: function (perms, callback) {
        FB.ui({
            method: 'permissions.request',
            perms: perms,
            display: 'popup'
        }, function (response) {
            if (response && response.perms) {
                if (callback) {
                    callback(true);
                }
            } else if (!response.perms) {
                if (callback) {
                    callback(false);
                }
            }
        });

    fql: function (q, callback) {
        FB.api({ "method": "fql.query", "query": q }, callback);
    },

    getOnlineFriends: function (callback) {
        var q = 'SELECT friends_online_presence FROM permissions WHERE uid=me()';
        c2p.facebook.fql(q, function (rows) {
            var isPermissionGranted = true;
            var q2 = "SELECT uid, name, pic_square, online_presence FROM user WHERE online_presence IN ('active', 'idle') AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY name";

            if (rows[0].friends_online_presence == 0) {
                isPermissionGranted = false;
                q2 = "SELECT uid, name, pic_square FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1=me()) ORDER BY name";
            }

            c2p.facebook.fql(q2, function (arr) {
                callback(arr, isPermissionGranted);
            });
        });
    }
}

// The way I call this code is:
c2p.facebook.getOnlineFriends(function (arr, isPermissionGranted) {
    $("#tmplFriends").tmpl(arr, { getIndex: function () { return $.inArray(this.data, arr); } }).appendTo("#friendsDiv");
    if (!isPermissionGranted) {
        $('.c2p_hidden').show();
    }
});

解决方法:

c2p.facebook.fql(q2,function(arr){…}如何看到回调变量?它很疯狂.
回调是getOnlineFriends函数的局部变量.

标签:javascript,facebook,facebook-fql
来源: https://codeday.me/bug/20190709/1418420.html