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 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    The Netherlands, Baarn, Ut.
    Posts
    4,252
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Atlanta, GA.
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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]

  • #5
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Atlanta, GA.
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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]


  •  

    LinkBacks (?)

    1. 07-19-2014, 08:51 AM

    Posting Permissions

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