View Full Version : Why does setInterval do it?

09-29-2003, 06:13 PM
I have a problem with the setInterval function
My line of code is:

The problem is that whatever I try the error message is:
this.particle.0 is null or not an object.

How can I make it work?
I know that this.particle[0].move() works. It doesnt only in the setInterval function.
It's probably a stupid question but I have no idea what to do.

09-29-2003, 06:50 PM
Did you try it without the quotes?


Just a guess but with quotes it would be treated as a string.

09-29-2003, 06:56 PM
I think if you quote it, it is still executed as code.

Can we see the rest of the code possibly?

09-29-2003, 06:56 PM
It says in the description of the function that the expression must be a quoted string...

09-29-2003, 06:57 PM
Both setInterval/setTimeout take a string of executable code as their first argument. You can't pass this because, when the timer runs, it refers to the window object (it's long gone from the function which launched it). You can save a reference to an object in a global variable - or an object property - or, better, start working with a Timer class like this (http://www.codingforums.com/showthread.php?threadid=10531&highlight=timer) that systematizes the process.

09-29-2003, 07:01 PM
Originally posted by fredmv
I think if you quote it, it is still executed as code.

Can we see the rest of the code possibly?

The code is a bit large but it was tested without the setInterval function and it worked without errors. (but it wont work the way I want it without setInterval();)
As I said this.particle[0].move() work's outside of setInterval();

09-29-2003, 07:03 PM
Not a stupid question at all. This stumps a lot of people trying to do OO setTimeout & SetInterval.

Think about when the function is executed.
if you do
then you are in the scope of the object

If you use
setInterval("this.particle[0].move()", 100);

Then when the interval fires 'this' no longer references your object.

The way I set up OO timers is to give the object an id, then use the id to create a global variable. You can then create your own timer routine to provide OO timeouts.

var idNo=0;

function myObj()
var id = "myObj" + idNo++;
this.id = id;
window[this.id] = this;

/*** Rest of object initialization ***/

myobj.prototype.setInterval = function(f, t)
/*** Construct a string like : "window['myObj1'].func()" ***/
setInterval("window['" + this.id + "']." + f, t);

You can then call it in your methods like this

this.setInterval("particle[0].move()", 100);

If you want you can do the var/id creation on one line

window[this.id = "myObj" + idNo++] = this;

09-29-2003, 07:06 PM
Both setInterval/setTimeout take a string of executable code as their first argument.

I think there's only one exception to that - If the first argument is a function pointer. I learned this from beetle... And it seems to be absolutely correct. This works perfectly:

<script type="text/javascript">
window.setTimeout(function(){ alert('Hello, World.'); }, 1000);

Interesting stuff. :D

09-29-2003, 07:16 PM
Point well taken. I was trying to keep things focused on the way in which timers are generally implemented and how this makes it tricky to pass object references as anything but literal (string) references.

I recommend Algorithm's approach, from personal use. Anything RoyW recommends is worthy of consideration, as well (take a look at his sites!). :cool:

09-29-2003, 07:47 PM
Maybe it's just me but it seems silly to have to put quotes around the command that you want to execute.

09-29-2003, 08:16 PM
Well, that was because of the fact that the code would execute if you didn't put it in a string in the first versions of JavaScript. Rememeber that the notion of function pointers in JavaScript isn't that very old.

Overall, the idea of synchronous or asynchronous time-based function execution didn't get much attention while writing the original JavaScript language.

09-29-2003, 08:30 PM
Just by the way: why is "this.particle[0]" turned into "this.particle.0" in the error?

09-29-2003, 09:04 PM
Bacause they are analogous in JScript (Microsoft's version), and ie use the internal representation of the script to present the error.

09-30-2003, 04:24 PM
Thanks to all of you!
I got the idea of it. I'll write my own code becouse it's hard to use with my code but I have what I wanted. Thanks again!:thumbsup:

09-30-2003, 07:45 PM
Another (this time really stupid) question.

When I try to get him to read the "this" from a global (or at lest it supposed to be becouse it's defined outside of any function) I get an error I'm missing a "]" bracket. Why?
The code:
(this is outside any function) var TimerID=myobj;
var a=this.setInterval(timerID + ".particle[0].move();",100);
it works if I do:
var a=this.setInterval("myobj.particle[0].move();",100);
I'm getting desperate...

09-30-2003, 07:56 PM
Well, to get your first example to behave like the 2nd, it should be (including a typo correction)
var TimerID = "myobj";
var a = this.setInterval( TimerID + ".particle[0].move();", 100 );
And yes, I'd do it this way
var self = this;
window.setInterval( function() { self.particle[0].move() }, 100 );
Or, this might work, but I don't think so
var self = this;
window.setInterval( self.particle[0].move, 100 );

09-30-2003, 08:34 PM
Brilliant! Beetle you did it what I could not. It finally works (the example #2).
Maybe this time I wont need any more help. :)
Thanks a lot!