...

View Full Version : setTimeout with OOP, how to keep instance?



Rexxars
07-02-2006, 08:32 PM
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 :p). 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 :p).



<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

Kravvitz
07-02-2006, 10:45 PM
I think you may need a global instance of the class in order to be able to do that.

jkd
07-03-2006, 06:52 AM
Scope is your friend.



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?


var interval = setInterval(function() {
//bla bla bla
if (some condition) {
clearInterval(interval);
}
}, 500);

That will repeat every 500 milliseconds until some condition is met.

Rexxars
07-03-2006, 08:27 AM
Thanks, that worked like a charm! So simple too. *shakes head*



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum