Ich habe die Verwendung von setTimeout vermieden, indem ich eine globale Funktion verwendet habe:
HINWEIS ZUR BEARBEITUNG: Ich habe die folgenden Hilfsskripte aktualisiert und eine Klasse erstellt, die einfacher zu verwenden ist; sehen Sie sich das hier an ::: https://github.com/tjmehta/fbExec.js
window.fbAsyncInit = function() {
FB.init({
//...
});
window.fbApiInit = true; //init flag
if(window.thisFunctionIsCalledAfterFbInit)
window.thisFunctionIsCalledAfterFbInit();
};
fbEnsureInit wird seinen Callback nach FB.init aufrufen
function fbEnsureInit(callback){
if(!window.fbApiInit) {
window.thisFunctionIsCalledAfterFbInit = callback; //find this in index.html
}
else{
callback();
}
}
fbEnsureInitAndLoginStatus wird seinen Callback nach FB.init und nach FB.getLoginStatus aufrufen
function fbEnsureInitAndLoginStatus(callback){
runAfterFbInit(function(){
FB.getLoginStatus(function(response){
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
callback();
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
});
}
Beispiel für die Verwendung von fbEnsureInit:
(FB.login muss ausgeführt werden, nachdem FB initialisiert wurde)
fbEnsureInit(function(){
FB.login(
//..enter code here
);
});
fbEnsureInitAndLogin Beispielverwendung:
(FB.api muss nach FB.init ausgeführt werden und der FB-Benutzer muss eingeloggt sein).
fbEnsureInitAndLoginStatus(function(){
FB.api(
//..enter code here
);
});