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 3 of 3

Thread: Recursion.

  1. #1
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Recursion.

    ok so apperently i don't know how to do this and i've searched google (for like 20 minutes) and can't find anything. i've probably passed over it and not realized.

    i want a method as part of my class to call itself until {condition} is met.

    however i keep getting Object Expected on pass #2

    function myClass(v1,v2)
    {
    this.n = 0;
    this.method = function()
    {
    this.n = Math.floor(this.n+1);
    alert('in the method, pass' + this.n);
    if(this.n < 10)
    window.setTimeout(function(){this.method},1000);
    }
    }
    window.onload = function()
    {
    var test = new myClass('v1','v2');
    test.method();
    }
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,505
    Thanks
    3
    Thanked 501 Times in 488 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    <!--
    var cnt=0;
    function myClass(v1,v2){
     this.n = 0;
    
     this.method = function(){
      this.n = Math.floor(this.n+1);
      this.ref='zxc'+cnt++;
      window[this.ref]=this;
      alert('in the method, pass' + this.n);
      if(this.n < 10){
       setTimeout('window.'+this.ref+'.method()',1000);
      }
     }
    }
    
    window.onload = function(){
     var test = new myClass('v1','v2');
     test.method();
    }
    
    //-->
    </script>
    </head>
    
    <body>
    
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    when you use setTimeout or setInterval, you change the executing context of the function. the "this" keyword always refers to the executing context. It's a dynamic reference to the executing frame. This is not what you want, you want a reference to the DEFINING context, you do this by creating a variable that stores "this" during deifition and using said variable.

    Code:
    function myClass(v1,v2)
    {
      var self = this;
      self.n = 0;
      self.method = function()
      {
        self.n = Math.floor(self.n+1);
        alert('in the method, pass' + self.n);
         if(self.n < 10)
           window.setTimeout(function(){self.method},1000);
       }
    }


  •  

    Posting Permissions

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