Guest_imported
New member
- Jan 1, 1970
- 0
<br>Hi,<br><br>I have been trying to call a function via setTimeout() method but a problem occurs in both major browsers. Before you look in the code, here is a description of what I like to do:<br><br>I want the function "myFunc()" be called every second and each time when myFunc() gets called I like the "this.width" to keep its value. I don't like it to become "undefined". Now if I do "setTimeout("myFunc()", 1000);" "this.width" becomes "undefined" the second time when the myFunc() gets called. So, I thought that to solve the problem I would write setTimeout("this.method()", 1000); but it doesn't work.<br><br>Here is the code which I have:<br><br><html><br><head><br><br><script language="JavaS.."> <br><br> // create a class<br> function Test(intW, intH) <br> {<br> this.width = intW;<br> this.height = intH;<br> <br> // assign a method......<br> this.method = myFunc; <br> <br> this.method(); // call the method<br> }<br><br> // define the method for the class<br> function myFunc() <br> {<br> // the first time when this function gets called <br> // from the constructor, this.width in here keeps <br> // the value...like a normal data member in a class..<br> <br> alert(this.width);<br><br> // as you see, this.width was initialated in<br> // the constructor above and then this member function<br> // gets called automatically by the constructor to modify<br> // the current object's data members/fields<br><br><br> // here I modify the current object's "width" property....<br> // which was declared and defined in the constructor above<br><br> this.width = this.width + 10;<br><br> <br><br> // Now, I call this.method() like this because the next time when <br> // this function get called, I want this.width to keep its value<br> // but not become "undefined"<br><br> // Please see the comment below....<br> setTimeout("this.method()", 1000); <br> }<br><br><br> <br> // <body onLoad = "hmm()"> ...... <br> function hmm()<br> {<br><br> d = new Test(10, 10);<br> }<br><br><br></script><br><br></head><br><br><body onLoad="hmm()"></body> </html><br><br><br>Now when I run the code, This is the error which I receive in Netscape: <br><br>"this.method is not a function.". As you can see the problem is in setTimeout() function.<br><br><br>Now if I do: setTimeout("myFunc()", 1000); -- that will work but "this.width" will become "undefined" the second time (after one second) when the function gets called and I don't want that. I want "this.width" to keep its value each time the function get called. Sure I can create one global variable but that's not what I like to do. This is what I like to implement without creating a global variable. <br><br>I want the function "myFunc()" be called every second and each time when myFunc() gets called I like the "this.width" to keep its value. I don't like it to become "undefined". Now as I said if I do "setTimeout("myFunc()", 1000);" "this.width" becomes "undefined" the second time when the myFunc() gets called. So, I thought that to solve the problem I would write setTimeout("this.method()", 1000); but it doesn't work.<br><br>Could you please suggest me something?<br><br><br>By the way, this problem prevents me from developing my DHTML object/class....<br><br>Thanks,<br><br>Regards:<br>Tonny9 <br><br> <br><br> <br>