Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-18-2002, 12:01 PM   PM User | #1
Beck
New Coder

 
Join Date: Jun 2002
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Beck is an unknown quantity at this point
custom objects & window.setTimeout

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
__________________
If at first you don't succeed, spend more time online researching javascript!
Beck
Beck is offline   Reply With Quote
Old 06-18-2002, 12:07 PM   PM User | #2
Beck
New Coder

 
Join Date: Jun 2002
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Beck is an unknown quantity at this point
a couple of corrections...

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.
__________________
If at first you don't succeed, spend more time online researching javascript!
Beck
Beck is offline   Reply With Quote
Old 06-18-2002, 12:40 PM   PM User | #3
ronaldb66
Senior Coder

 
Join Date: Jun 2002
Location: The Netherlands, Baarn, Ut.
Posts: 4,253
Thanks: 0
Thanked 0 Times in 0 Posts
ronaldb66 is an unknown quantity at this point
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.
__________________
Regards,
Ronald.
ronaldvanderwijden.com
ronaldb66 is offline   Reply With Quote
Old 06-18-2002, 10:43 PM   PM User | #4
RoyW
Regular Coder

 
Join Date: Jun 2002
Location: Atlanta, GA.
Posts: 313
Thanks: 0
Thanked 0 Times in 0 Posts
RoyW is an unknown quantity at this point
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;

    
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+"."+ft);
}

// 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]
RoyW is offline   Reply With Quote
Old 06-19-2002, 08:14 AM   PM User | #5
Beck
New Coder

 
Join Date: Jun 2002
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Beck is an unknown quantity at this point
confirmations

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
Beck is offline   Reply With Quote
Old 06-21-2002, 05:01 PM   PM User | #6
RoyW
Regular Coder

 
Join Date: Jun 2002
Location: Atlanta, GA.
Posts: 313
Thanks: 0
Thanked 0 Times in 0 Posts
RoyW is an unknown quantity at this point
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]
RoyW is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:57 AM.


Advertisement
Log in to turn off these ads.