1

Background:
I've written a javascript object called Step to implement a step-type event for any operation I might need to use for my website, e.g. smooth-motion functions. I can register functions in it to run each step by using the Step object's method, registerFunction(func).

To handle running the functions every step, the Step object has another method called run(). In this method I run through the list of registered functions, run them, and then call the setTimeout(...) function, passing the reference to the run function to recall it for the next step.

Problem:
The first 'step' is run by the onload event (attribute) in the <body> tag of my html page (indirectly, though: Step.run is called by a function initiateJS() which is called by onload). Nothing goes wrong in the first step. However, by the second step, the variables within the Step object seem to have been disposed; they all become undefined.

Questions:
I was thinking it might be due to the garbage collector? Maybe it somehow loses its reference?
Also, is using setTimeout(...) even the best method of making a step-type event implementation?

Details:
The Step object is declared in the js document somewhat as:

    var Step = {
            myVars: "somevalues",
            anArray: [],
            run: function() {
                //run the registered functions
                setTimeout(this.run,this.interval);
        };

ALSO, in the next step, the Step object is still existent.

Some other minor details:

  • I'm using Chrome
  • I'm making my website with XAMPP full
  • I'm using Windows 7
  • 1
    • Is there a reason why you don't want to use setInterval? Commented May 3, 2012 at 22:55

    2 Answers 2

    1

    setTimeout runs code in the global context. this is no longer defined the next time run() executes. You'll need to refactor your code to either declare variables on some kind of global object, or to pass references into the run function itself.

    edit: since you said Step is global, this should work:

    run: function() {
        //run the registered functions
        setTimeout(Step.run, Step.interval);
    }
    
    Sign up to request clarification or add additional context in comments.

    1 Comment

    How would I declare a global object that would preserve its variables?
    1

    Change your run function to this:

    run: function() {
        var that = this;
        setTimeout(that.run, that.interval);
    }
    

    The setTimeout function redefines the this variable and this is the simplest way I can think of to work around this issue.

    3 Comments

    Won't this still break, because this is still undefined the next run is executed? And so that will be set to undefined?
    Tried it and it didn't work. And I don't see how this answers the problem at all.
    Rather, this is the window object next time it's run. Putting var that = this in the function accomplishes nothing, since you're effectively doing var that = window, and window.run is still undefined.

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.