// 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
__________________
If at first you don't succeed, spend more time online researching javascript!
Beck
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.
__________________
If at first you don't succeed, spend more time online researching javascript!
Beck
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
PHP Code:
var IdNo = 0;
// create custom object
function myObject () {
//Save a reference to myself
this.id = "myObject" + IdNo++;
window[this.id] = this;
// 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
__________________
The answer does not come from thinking outside the box, it comes from realizing the truth :-
"There Is No Box". [JavaScript Gadgets'n'Gizmos][JavaScript-FX]
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!
__________________
If at first you don't succeed, spend more time online researching javascript!
Beck
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)
__________________
The answer does not come from thinking outside the box, it comes from realizing the truth :-
"There Is No Box". [JavaScript Gadgets'n'Gizmos][JavaScript-FX]