使用FB.logout避免Facebook JavaScript API中的竞争条件
我正在拼命尝试在用户开始新的facebook会话之前强制注销。但是在成功调用logout之前,auth.login似乎要登录用户,我将其重定向。所以这似乎是一种比赛状态,但我不知道如何克服它使用FB.logout避免Facebook JavaScript API中的竞争条件,javascript,facebook,race-condition,Javascript,Facebook,Race Condition,我正在拼命尝试在用户开始新的facebook会话之前强制注销。但是在成功调用logout之前,auth.login似乎要登录用户,我将其重定向。所以这似乎是一种比赛状态,但我不知道如何克服它 FB.init({appId: xxxx, status: true, cookie: true, xfbml: true}); //Try to log out user FB.logout(); //Show the FB div if(FB)
FB.init({appId: xxxx, status: true, cookie: true, xfbml: true});
//Try to log out user
FB.logout();
//Show the FB div
if(FB) {
FB.Event.subscribe('auth.logout', function(response) {
// do something with response
logout();
});
FB.getLoginStatus(function(response) {
if (response.session) {
login(response);
}
});
FB.Event.subscribe('auth.login', function(response) {
login(response);
//login redirects the user. Before logout fires()
});
} // end if(FB).
可能是这样的:
//initial login check
FB.getLoginStatus(function(response) {
if (response.session) {
//logged in, force logout
FB.logout(function() {
//logged out, subscribe to events
loginEvents();
});
} else {
//not logged in
loginEvents();
}
});
function loginEvents() {
FB.Event.subscribe('auth.logout', function(response) {
// do something with response
logout();
});
FB.Event.subscribe('auth.login', function(response) {
login(response);
//login redirects the user. Before logout fires()
});
}