tags:

views:

90

answers:

2

i have this block of code:

$(document).ready(function() {
    //<![CDATA[  
    var who;

    FB_RequireFeatures(["Api"], function(){ 

        var who = api.get_session().uid;
        alert(who);

        });

        alert("the uid is: "+who);

    //]]> 
});

the problem: the code outside the FB_RequireFeatures block is executing before the one inside it. due to which the value of who is coming out to be undefined.

what am i doing wrong?

+4  A: 
Tatu Ulmanen
how to make sure the code that follows the function gets executed after FB_RequireFeatures is done requesting?
amit
@amit, I updated my answer with an example.
Tatu Ulmanen
that does help things a bit. but can something be done to make the code after the FB_RequireFeatures to wait explicitly for FB_requireFeatures to finish?
amit
i tried the above method. for some reason the code in someotherfunction() is still getting executed before the ajax call completes?
amit
@amit: no, you can't turn asynchronous execution code into synchronous or vice-versa. All code you want to run when `FB_RequiresFeatures` has finished must be passed to it as a callback function.
bobince
+2  A: 

You're making a new local who variable. Remove the var from the place where you set who. Also, you can't reference who until the callback to the FB_RequireFeatures function is run.

Eli Grey