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?
The
FB_RequireFeaturesfunction appears to be making an asynchronous call, so you’re not doing anything wrong, that’s just the way it works – the alert is called before the request comes back.You must design your code in a way that the code that depends on the result of the
FB_RequireFeaturesfunctions are called only after the request completes. You can call another function inside the callback, for example:Now the
doSomeOtherStufffunction is called only after theFB_RequireFeaturesfunction finishes, and you should do all following code inside thedoSomeOtherStufffunction – which you can name to be anything you want, obviously.I moved the
whovariable out of the ready block to keep it in scope for thedoSomeOtherStufffunction, and removed thevarfrom the inner function so that you’re referencing the original variable instead of creating a new one, otherwise it’s the same.