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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Why does setInterval do it?

    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.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think if you quote it, it is still executed as code.

    Can we see the rest of the code possibly?
    Moderator @ WebDeveloper.com
    Mentor @ WebXpertz.net

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It says in the description of the function that the expression must be a quoted string...

  • #5
    Regular Coder
    Join Date
    Aug 2003
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 that systematizes the process.
     ........ another wild guess ........

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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();

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Atlanta, GA.
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Code:
    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
    Code:
    this.setInterval("particle[0].move()", 100);
    If you want you can do the var/id creation on one line
    Code:
        window[this.id = "myObj" + idNo++] = this;

  • #8
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    <script type="text/javascript">
    	window.setTimeout(function(){ alert('Hello, World.'); }, 1000);
    </script>
    Interesting stuff.
    Moderator @ WebDeveloper.com
    Mentor @ WebXpertz.net

  • #9
    Regular Coder
    Join Date
    Aug 2003
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!).
    Last edited by cheesebag; 09-29-2003 at 06:18 PM.
    &nbsp;........ another wild guess ........

  • #10
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Maybe it's just me but it seems silly to have to put quotes around the command that you want to execute.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #11
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #12
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Just by the way: why is "this.particle[0]" turned into "this.particle.0" in the error?

  • #13
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Bacause they are analogous in JScript (Microsoft's version), and ie use the internal representation of the script to present the error.
    Last edited by liorean; 09-29-2003 at 08:09 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #14
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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!

  • #15
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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...


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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