...

View Full Version : custom objects & window.setTimeout



Beck
06-18-2002, 01:01 PM
Consider the following code:

// create custom object
function myObject () {
this.count = 5;
this.countDown = countDown;
}

// member of myObject
function countDown () {
window.status = "Counter: "+this.count;
this.count--;
window.setTimeout("this.countDown()",200);
}

// create instance of myObject
var myCounter = new myObject ();
myCounter.countDown ();

// end of code

I call countDown using the myCounter instance of myObject. Window status bar displays a 5. All good so far.
window.setTimeout calls myCounter instance of myObject again, right? That was my intention, but it appears that when myCounter is executed the second time, it is no longer associated with myCounter, and the request for this.count is undefined at best.
How do I use window.setTimeout to call functions while maintaining their association to calling objects?

Thanks in advance!
Beck

Beck
06-18-2002, 01:07 PM
A couple of corrections for my previous post:

when I said that myCounter is no longer associated with myCounter (which of course makes no sense), I meant that countDown (member of myCounter) is no longer associated with myCounter. In other words, window.setTimeout calls countDown a second time, and my intent was to have it again associated with myCounter (so I used this.countDown). However, when it calls again, it no longer appears connected with myCounter, so this.count appears undefined.
How do I keep objects and their member functions associated throughout the use of window.setTimeout?

Sorry about the double post.

ronaldb66
06-18-2002, 01:40 PM
Beck,

i did some reading up on the subject:
http://www.javascriptkit.com/javatutors/object4.shtml
, and it looks like you've declared the method and object the wrong way around. Hope this helps.

RoyW
06-18-2002, 11:43 PM
When the timer fires you are no longer in your object so "this" wouldn't reference anything anyway. You have to save a reference to your object in the window object so you can set the timeout to execute the function
"window.myObject.method()"

To do this I save a reference to the object and create my own set timeout method for the object



var IdNo = 0;

// create custom object
function myObject () {
//Save a reference to myself
this.id = "myObject" + IdNo++;
window[this.id] = this;

this.count = 5;
this.countDown = countDown;
this.setTimeOut = mySetTimeout;
}

// member of myObject
function countDown () {
window.status = "Counter: "+this.count;
this.count--;
this.setTimeout("countDown()",200);
}
function mySetTimeout (f,t)
{
setTimeout("window."+this.id+"."+f, t);
}

// create instance of myObject
var myCounter = new myObject ();
myCounter.countDown ();

// end of code

Beck
06-19-2002, 09:14 AM
Thanks, that's some good looking code. I have some questions. Here's a modified version of what's printed above:

var IdNo = 0;

// create custom object
function myObject () {
//Save a reference to myself
this.id = "myObject" + IdNo++;
window[this.id] = this;

this.count = 5;
this.countDown = countDown;
}

// member of myObject
function countDown () {
window.status = "Counter: "+this.count;
this.count--;
setTimeout("window."+this.id+".countDown()",200);
}

// create instance of myObject
var myCounter = new myObject ();
myCounter.countDown ();

// end of code

The only real difference is that countDown again does the same work as originally intended, only it does it using the window object as a "storage".

What I'm asking is this: Is there any reason I can't do it this way, or do I need to do it through the use of two different function (both member functions) as demonstrated in the previous post by Roy?

Sorry so confusing, but the terminology is tough to use appropriately. Thanks for the help. You guys (no gender intended) are amazing!

RoyW
06-21-2002, 06:01 PM
Hi,
That should work perfectly. There is no reason to create another member function if you don't want to. The line

setTimeout("window."+this.id+".countDown()",200);

simply replaces the method call.

(I like to create a method so when I read the code I see "this.setTimeout" only as a readability issue. It kinda shows it is a self referencing timer)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum