Here's the deal: I want my doAjaxRequest function to open a CFM file, which then calls a function in a CFC file depending on the ?functions= url. Well, everything works perfectly, except result. The particular thing I'm doing is when a user is typing in a desired username, onkeyup calls this function which then makes a database query to see if the username is already taken. The thing is, inside the onreadystatechange function, result is updated as intended, but when i get out of that, and return result, it's always one result behind.

For example, they type in "test", but "test" is taken. But, instead of it returning false when they type the second "t", it says true. But if I backspace or type another letter, it will say false. The same is for the very first keystroke, except instead of saying true, it will return undefined(null, i suppose).

Any ideas?


Code:
var result;
function doAjaxRequest(type, functions, location)
{
	aXmlHttp.open(type, "/ajax/ajax.cfm?"+functions, true);
	aXmlHttp.onreadystatechange = 
	function()
	{
		if (aXmlHttp.readyState == 4) 
		{
			if (aXmlHttp.responseText) 
			{
				var a = aXmlHttp.responseText;
				if(location != null)
				{
					document.getElementById(location).innerHTML = a;
				}
				else
				{
					result = a;
				}
			}
		}
	};
	aXmlHttp.send(null);
	
	return result;
}