Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    setTimeout with OOP, how to keep instance?

    Hi.

    I'm trying to create some code that, when you click a button, it sends an AJAX request to the server and gets back a status reply (WAIT! This is not an AJAX problem ). Now, based on the reply (let's say it returns 'not done') I want it to send another AJAX request to the server 500 ms later, asking for the status again.

    Now, all of this works fine, except that I want this to be 'threaded', meaning you can click on the button again at a later time (after the script is done, and without reloading the page) and have a new ID sent to the server.
    Here's where I encounter the problem:

    I need this timer to keep it's "instance" of the class, so that I don't have to pass the ID as an argument to all the different functions, but rather just keep it as a variable in the class. Here's some sample code to explain what I mean, the AJAX functions are not included, because they work, I just need to find out how the setTimeout can "keep the instance" (sorry if this is poorly explained, I'm norwegian ).

    Code:
    <html>
    <head>
    	<title>Test code</title>
    	
    	<script>
    	function MyClass()
    	{
    		// Generate a unique ID
    		var oDate = new Date();
    		this.iID = oDate.getTime();
    		
    		this.doAjax = doAjax;
    		this.updateStuff = updateStuff;
    		this.dontUpdate = dontUpdate;
    		
    		function doAjax(bCall)
    		{
    			// Set the GET arguments
    			var sGetData = '?id=' + this.iID;
    			
    			if(bCall == 'true')
    			{
    				// Send AJAX-request here, callback function is updateStuff
    				sendAjax('index.php' + sGetData, updateStuff);
    			}
    			else
    			{
    				// Send AJAX-request here, callback function is dontUpdate
    				sendAjax('index.php' + sGetData, dontUpdate);
    			}
    		}
    		
    		function updateStuff(sStatus)
    		{
    			// Checks if we're done
    			if(sStatus == 'done')
    			{
    				// Do whatever needs to be done
    			}
    			else if(sStatus == 'nearly done')
    			{
    				setTimeout('doAjax(false)', 500);
    			}
    			else
    			{
    				// We're not done, so make a new AJAX call after 500ms
    				
    				// Here's the problem, doAjax is of course undefined
    				// and oMyClass.doAjax is also undefined, what do I use?
    				setTimeout('doAjax(true);', 500);
    			}
    		}
    		
    		function dontUpdate()
    		{
    			// Don't update stuff :p
    		}
    	}
    	
    	function startIt()
    	{
    		// Starts the uploading
    		oMyClass = new MyClass();
    		
    		// Call the status update process
    		oMyClass.doAjax(true);
    	}
    	</script>
    </head>
    <body>
    
    <input type="button" onClick="startIt();" value="ClickMe!">
    
    </body>
    </html>
    Any ideas? This is driving me nuts..

    Thanks,
    - Rexxars

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think you may need a global instance of the class in order to be able to do that.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Scope is your friend.

    Code:
    setTimeout(function() { doAjax(true/false) }, 500);
    That will know what particular `doAjax` method was in scope during the execution.

    Alternatively, why not just do a setInterval instead?
    Code:
    var interval = setInterval(function() {
        //bla bla bla
        if (some condition) {
            clearInterval(interval);
        }
    }, 500);
    That will repeat every 500 milliseconds until some condition is met.

  • #4
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, that worked like a charm! So simple too. *shakes head*


  •  

    Posting Permissions

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