kabirbaidhya
kabirbaidhya

Reputation: 3490

Javascript: How to detect facebook connection timed out error

I'm using Facebook Login in website with Javascript SDK. We start things like:

window.fbAsyncInit = function() {

    FB.init({
      appId      : '{your-app-id}',
      xfbml      : true,
      version    : 'v2.0'
    });

    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });

  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));

Currently, I'm showing loading animation while login status is being loaded; and after that the user's page is loaded. So, everything works as expected if facebook is working. But in case fb is not accessible, or connection error the loading anim is shown continuously but nothing happens; in console it shows connection timed out error.

So, all I want to do is to detect this situation when it occurs so that I can show message "Can't connect to facebook". Because if this problem occurs my app simply won't proceed. What is the best way to do this?

Upvotes: 0

Views: 1736

Answers (1)

kabirbaidhya
kabirbaidhya

Reputation: 3490

I just found a way out of this situation.

After some timeout of trying connection if still no successful connection then show a message Can't connect with Facebook Server. Try Again?

var facebookConnectSuccess = false;

window.fbAsyncInit = function() {

  FB.init({
    appId      : '{your-app-id}',
    xfbml      : true,
    version    : 'v2.0'
  });

  checkFacebookLoginStatus();
};

//Check after 1 minute
var connectionTimeout = 60000;
setTimeout(function() {
    if(!facebookConnectSuccess) {
       var tryagain = confirm("Can't connect with Facebook Server. Try Again?");
       if(tryagain) {
          checkFacebookLoginStatus();
       }
    }
}, connectionTimeout);

function checkFacebookLoginStatus() {
    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);

      facebookConnectSuccess = true;

    });
}

Upvotes: 2

Related Questions