CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2004
    Posts
    57

    Function not returning newest variable

    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;
    }

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Function not returning newest variable

    Well, first off there is an AJAX forum. I will have a moderator move it there.

    Quote Originally Posted by Psytherium
    it's always one result behind.
    Sounds to me like you are experiencing something similar to JavaScripts onkeypress vs. onkeyup.

    Is this the same thing you are battling?
    Code:
    This is one behind when typing.
    <input type="text" onkeypress="alert(this.value)">
    This is on time.
    <input type="text" onkeyup="alert(this.value)">
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Sep 2004
    Posts
    57

    Re: Function not returning newest variable

    EDIT: Well, I took a glance at the AJAX forum and saw a thread that talked about SJAX(SynchronousJAX), so I gave it a try and it works. Trying to learn client-side coding, server-side coding, and conventions like AJAX all at the same time is pretty difficult, ahhh.

    Perhaps it is a onkeyup/onkeypress problem. I tried changing my
    document.getElementById("username").onkeyup = validateUsername
    to
    document.getElementById("username").onkeypress = validateUsername,

    and now when I type in the username textbox, nothing happens. I did run this test:

    Code:
    var testResult = 0;
    function testResultF()
    {
    	document.onkeyup = 
    	function()
    	{
    		var a = testResult + 1;
    		testResult = a;
    		document.getElementById("test").innerHTML = testResult;
    		document.getElementById("test").innerHTML += a;
    	}
    	
    	return testResult;
    }
    The code above returns one value behind.

    Code:
    var testResult = 0;
    function testResultF()
    {
    	document.onkeypress = 
    	function()
    	{
    		var a = testResult + 1;
    		testResult = a;
    		document.getElementById("test").innerHTML = testResult;
    		document.getElementById("test").innerHTML += a;
    	}
    	
    	return testResult;
    }
    The code above returns the proper result.

    I just don't see why if inside the nested function testResult prints out the desired value, why it isn't returned.
    Last edited by Psytherium; October 2nd, 2006 at 10:18 AM.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Function not returning newest variable

    Quote Originally Posted by Psytherium
    I just don't see why if inside the nested function testResult prints out the desired value, why it isn't returned.
    Well, in your examples, the return call is not inside the function that contains the variable to be returned.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Sep 2004
    Posts
    57

    Re: Function not returning newest variable

    Quote Originally Posted by peejavery
    Well, in your examples, the return call is not inside the function that contains the variable to be returned.
    But if the variable is global, should it not matter? Or is java script different than other programming languages in that sense?

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: Function not returning newest variable

    I guess I have never fully tested it.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured