0

我正在向 facebook 发送 FB.login 请求。但 FB 未定义,而 javascript SDK 仍在加载核心 javascript 资源。

所以,我检查了获取 FB 变量

function check_FB_variable(){
            if(typeof FB=='undefined'){
                check_FB_variable();
            }else{}
           }
           check_FB_variable();

但是这种方法给了我太多的递归错误。

所以,我把这段代码写成

function check_FB_variable(){
            if(typeof FB=='undefined'){
                setTimeout(check_FB_variable,600);
            }else{}
           }
           check_FB_variable();

但在这种方法中,超时前函数调用函数向下移动并给出错误 FB.login 未定义。

请帮忙。

4

1 回答 1

0

我使用了类似的东西来检查 JQMobi 是否存在,我不知道确切原因,但我认为抛出异常是因为你每次都调用指向函数的指针。

您应该尝试检查这样的间隔(未经测试): var facebookChecker = window.setInterval(fbCheck, 200);

var fbCheck = function () {
    if (typeof FB != 'undefined' && facebookChecker != null) {
        window.clearInterval(facebookChecker);
        facebookChecker = null;

        // Whatever you want to do if facebook is loaded
        // Example: InitFBLogin();
    }
}

或者您可以使用 while 语句(我使用的那个):

/*
*   This JQ Fix tries to attach Jquery to a variable to ensure it exists.
*   - Marvin Brouwer.
*/

var FixJQ = function () {
    var JQFIX = null;
    while (!JQFIX || JQFIX == null) {
        try {
            JQFIX = jQuery;
        } catch (nothing) {  jQuery =  $; };
    };
    JQFIX = null;
    return true;
};

if (FixJQ()) {
    FixJQ = null;
};

最后一个的美妙之处在于,您可以将下一步放在下面,因为它会一直等到 while 循环完成。老实说,我不知道哪个更好/更快,但我相信底部的那个会起作用。

于 2013-05-08T10:23:42.890 回答