Facebook API called onlogin

Can someone tell me how the FB API works. This seems like a basic question, but I'm really confused.

Question: I have onlogin (). When I press the login button, I expect it to call this function. But in the code I pasted: I see that the test test first prints and FB.api is called.

So it looks like onlogin is being called first, then the FB API ... there is a way that I can call this function only once.

<body> <div id="fb-root"></div> <script> window.fbAsyncInit = function() { FB.init({appId: 'XXX', status: true, cookie: true, xfbml: true}); }; (function() { var e = document.createElement('script'); e.async = true; e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; document.getElementById('fb-root').appendChild(e); }()); function checkFacebookLogin() { FB.api('/me', function(response) { alert("Name: "+ response.name + "\nFirst name: "+ response.first_name + "ID: "+response.id); }); alert('test'); } </script> <p id="fb_login_button_1"><fb:login-button onlogin="checkFacebookLogin();" size="medium" scope="user_about_me">Sign in using Facebook</fb:login-button></p> </body>v 

My main problem is that the function should be called only once .... but it is called twice.

+7
source share
2 answers
  <body> <div id="fb-root"></div> <script> window.fbAsyncInit = function() { FB.init({appId: 'XXX', status: true, cookie: true, xfbml: true}); }; (function() { var e = document.createElement('script'); e.async = true; e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; document.getElementById('fb-root').appendChild(e); }()); function fetchUserDetail() { FB.api('/me', function(response) { alert("Name: "+ response.name + "\nFirst name: "+ response.first_name + "ID: "+response.id); }); } function checkFacebookLogin() { FB.getLoginStatus(function(response) { if (response.status === 'connected') { fetchUserDetail(); } else { initiateFBLogin(); } }); } function initiateFBLogin() { FB.login(function(response) { fetchUserDetail(); }); } </script> <input type="button" value="Sign in using Facebook" onclick="checkFacebookLogin();"/> </body> 
+8
source

The request to call the FB API is asynchronous. This means that after sending the request, the code will not wait for the request to complete. Because of this, you get a test signal before returning the API call. All FB API calls are asynchronous, if you try to make them synchronous then your browser will hang.

Thanks Koshik

+1
source

All Articles