更新时间:2023-12-06 15:39:46
2012 年 1 月 4 日更新
似乎您不能像以前一样在 FB.init()
之后立即调用依赖于 FB 的方法(例如 FB.getAuthResponse()
),因为FB.init()
现在似乎是异步的.将您的代码包装到 FB.getLoginStatus()
响应中似乎可以检测 API 何时完全就绪:
It seems like you can't just call FB-dependent methods (for example FB.getAuthResponse()
) right after FB.init()
like before, as FB.init()
seems to be asynchronous now. Wrapping your code into FB.getLoginStatus()
response seems to do the trick of detecting when API is fully ready:
window.fbAsyncInit = function() {
FB.init({
//...
});
FB.getLoginStatus(function(response){
runFbInitCriticalCode();
});
};
或者如果使用下面的 fbEnsureInit()
实现:
or if using fbEnsureInit()
implementation from below:
window.fbAsyncInit = function() {
FB.init({
//...
});
FB.getLoginStatus(function(response){
fbApiInit = true;
});
};
原帖:
如果你只想在 FB 初始化时运行一些脚本,你可以在 fbAsyncInit
中放入一些回调函数:
If you want to just run some script when FB is initialized you can put some callback function inside fbAsyncInit
:
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $conf['fb']['appid']; ?>',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Canvas.setAutoResize();
runFbInitCriticalCode(); //function that contains FB init critical code
};
如果你想完全替换 FB.ensureInit 那么你必须自己写一些东西,因为没有官方替换(imo 大错).这是我使用的:
If you want exact replacement of FB.ensureInit then you would have to write something on your own as there is no official replacement (big mistake imo). Here is what I use:
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $conf['fb']['appid']; ?>',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Canvas.setAutoResize();
fbApiInit = true; //init flag
};
function fbEnsureInit(callback) {
if(!window.fbApiInit) {
setTimeout(function() {fbEnsureInit(callback);}, 50);
} else {
if(callback) {
callback();
}
}
}
用法:
fbEnsureInit(function() {
console.log("this will be run once FB is initialized");
});