Web Analytics Made Easy -
StatCounter custom objects & window.setTimeout - CodingForum

Announcement

Collapse
No announcement yet.

custom objects & window.setTimeout

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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

    Comment


    • #3
      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

      Comment


      • #4
        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]

        Comment


        • #5
          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

          Comment


          • #6
            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]

            Comment

            Working...
            X