I have a problem with the setInterval function
My line of code is:
window.setInterval("this.particle[0].move()",100);

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.

Spookster

09-29-2003, 05:50 PM

Did you try it without the quotes?

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

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

fredmv

09-29-2003, 05:56 PM

I think if you quote it, it is still executed as code.

Can we see the rest of the code possibly?

marek_mar

09-29-2003, 05:56 PM

It says in the description of the function that the expression must be a quoted string...

cheesebag

09-29-2003, 05: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.

marek_mar

09-29-2003, 06: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();

RoyW

09-29-2003, 06: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
this.this.particle[0].move();
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.
e.g.

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:

Spookster

09-29-2003, 06:47 PM

Maybe it's just me but it seems silly to have to put quotes around the command that you want to execute.

liorean

09-29-2003, 07: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.

marek_mar

09-29-2003, 07:30 PM

Just by the way: why is "this.particle[0]" turned into "this.particle.0" in the error?

liorean

09-29-2003, 08:04 PM

Bacause they are analogous in JScript (Microsoft's version), and ie use the internal representation of the script to present the error.

marek_mar

09-30-2003, 03: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:

marek_mar

09-30-2003, 06: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);
Why?
it works if I do:
var a=this.setInterval("myobj.particle[0].move();",100);
I'm getting desperate...

beetle

09-30-2003, 06: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 );

marek_mar

09-30-2003, 07: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!