This feels like a quite noob question, but what the heck. I'm sitting with a PhoneGap project and having some problems.I followed a tutorial for a small twitterfeed with ajax (http://wiki.phonegap.com/w/page/42450600/PhoneGap%20Ajax%20Sample) and it works like a charm. But, i am coding an smal API for logging in, logging out etc and etc and i almost got it to work.I'll explain my problem:i set up for example a BOOL variable, "b" that determines if the user is logged in or not. var b = Do_Ajax('isloggedin');and the function looks almost exactly like this:\[code\] function Do_Ajax(what) { var ajax = new XMLHttpRequest(); if(what == "isloggedin") { ajax.open("GET", root() + "/?isloggedin",true); ajax.send(); } //Get the results back ajax.onreadystatechange=function() { if(ajax.readyState==4 && (ajax.status==200||ajax.status==0)) { eval('var data = 'http://stackoverflow.com/questions/9600982/+ ajax.responseText +';'); var theResults = data.results; if(data['BOOL'] != null ) { return data['BOOL']; } } } }\[/code\]If i now try an alert on this variable (b), i get "undefined". If i change "return" to an "alert" (where i print "return data['BOOL'];" - the php returns a json string) i will get the true or false value. If i then have an alert on the b variable AND in the function - the b variable alerts first and then the data['BOOL'] alerts. So the problem is that the function returns undefined before it gets the data. How can i make the function "wait" to return this value until it has run that onreadystatechange stuff?EDIT: One way to solve it is to declare a variable outside the function, like "var IsUserOnline = false;" and then in the function when the request it done set "IsUserOnline = data['BOOL']:" and later in the code just use this variable. But nah, i think the return-thinking would be best. I think my code will be very messy in the future when i will get/post more stuff via this function. Maybe it's just my thinking thats really wrong now?