SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Aug 15, 2007, 12:50 #1
- Join Date
- Jun 2006
- Location
- Uttoxeter, Staffordshire, UK
- Posts
- 38
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
How to setTimeout("this.doSomething()",10)?
Hello,
I have a custom object and in one of the methods of this object, I need to setTimeout to the method it's currently executing. Basically, so far, I've got:
Code JavaScript:setTimeout("this.move()",10);
The only way I can think of solving this is to record all of the instances into an array and use a global function to target the correct instance, but I would prefer to get this to work...
Thanks,
Aquis
-
Aug 15, 2007, 13:00 #2
- Join Date
- Oct 2004
- Location
- Birtley, UK
- Posts
- 2,439
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
try
Code:setTimeout(function() { this.move(); }, 10);
-
Aug 15, 2007, 13:02 #3
- Join Date
- Jul 2007
- Posts
- 345
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Before calling setTimeout you can assign this to another variable and use that instead.
Code:var me = this; setTimeout(function(){me.move()}, 10);
-
Aug 15, 2007, 13:07 #4
- Join Date
- Jun 2006
- Location
- Uttoxeter, Staffordshire, UK
- Posts
- 38
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
gRoberts - Really thought that was going to work for a second there, but it unfortunately did not...
r51 - The problem with this is that quite a few instances will all be doing this. Actually, that might work as an array, but it could get big and messy, however I'll try that for now...
Thanks!
-
Aug 15, 2007, 18:34 #5
- Join Date
- Mar 2004
- Location
- Earth
- Posts
- 406
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
what r51 said is what you need to do - "this" will point to window inside an interval, so you need to copy the reference into the parent scope and use that, exactly as demonstrated
-
Aug 16, 2007, 04:03 #6
- Join Date
- Jun 2006
- Location
- Uttoxeter, Staffordshire, UK
- Posts
- 38
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hmm, okay, I'll give it a try then...
Robert
Edit: I guess you were right, it worked! I'll have to bear that in mind in future...
Thanks All!
Bookmarks