0

I try do this :

<script>
    FB.getLoginStatus(function(response) {  
        if (response.status==='connected') {
            var fb_status="on";
        } else {
            var fb_status="off";    
        }
    });

    alert("ok"+fb_status);

</script>

If i put alert inside function , this alert works , by other side , if i put this alert outside , no works

i supose this it´s more or less the same as global vars in php but no have many experience in Javascript

I only need the same i put in the example for detect in a var the result from function but as in the case i put as example , outside function , i try many things but no get results

Regards and thank´s for the help to community

adeneo
  • 312,895
  • 29
  • 395
  • 388

2 Answers2

0

FB.getLoginStatus is async function so when you try getting fb_status outside it, you get undefined because FB.getLoginStatus didn't return any result yet

Pavel Komiagin
  • 708
  • 6
  • 10
0

As the FB.getLoginStatus call is asynchronous, the result only occurs later and is only relevant inside the event handler:

e.g.

<script>
    FB.getLoginStatus(function(response) 
    {   
        // This line gets called many millisconds later
        console.log("When I have a status");
        if (response.status==='connected') 
        {
           var fb_status="on";
        }
        else
        {
            var fb_status="off";    
         }
         alert("ok"+fb_status);
    });
    // This line executes long before the callback returns
    console.log("After call");
</script>

Check out the console to see the order of events:

  • "After call"
  • "When I have a status"

You mention in comments you want to use the value elsewhere. The problem will be the timing as the result you want will only occur later.

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202