2

When I try to run this program, I get an error in Firefox saying that:

moveDate is undefined on line 41

(referring to the line window.setTimeout("moveDate()",100);.

Any ideas why? I thought recursive functions were able to define themselves and then call upon themselves.

function monthScroller(){
    document.getElementById("month").style.visibility = "visible";
    var x = 0;
    var y = 0;
    var dest_x = window.innerWidth/2;
    var dest_y = window.innerHeight/2;  
    var interval = 1;

    function moveDate() {
        if(x<dest_x){ x = x + interval;} 
        if(y<dest_y){ y = y + interval;}

        document.getElementById("month").style.top  = y+"px";
        document.getElementById("month").style.left = x+"px";

        if ((x+interval < dest_x) && (y+interval < dest_y)) {
                window.setTimeout("moveDate()",100);
        }
        else{
            name();
        }
    }
    moveDate();
}
4
  • 1
    eval() is evil. Don't use those, including that one in setTimeout(""). Commented May 30, 2012 at 3:56
  • Derek, you beat me to the eval() is evil line. Anyway, this blog post explains more about why you don't want to use strings to evaluate code dynamically (which is what setTimeout does when you use a string). blogs.msdn.com/b/ericlippert/archive/2003/11/01/53329.aspx Commented May 30, 2012 at 3:57
  • 3
    It's not really recursive, moveDate() will complete its call about 100ms before it is run again from your setTimeout call if you follow the advice of the answerers. Commented May 30, 2012 at 3:58
  • @Derek—give it a rest. The "eval is evil" mantra started over a decade ago and was fundamentally not about evaluating strings in setTimeout and setInterval (and Function), they were side issues. The only problem with its use here relates to efficiency (ignoring the scope issue), none of the other "evilness" of eval is relevant here. Commented May 30, 2012 at 5:02

2 Answers 2

6

Yes, they are. Yet, window.setTimeout("moveDate()",100); will eval that code string in the global scope - no moveDate to be found there. Instead, pass the function reference to setTimout():

 window.setTimeout(moveDate, 100);
Sign up to request clarification or add additional context in comments.

Comments

2

"moveDate" is scoped inside of the monthScroller method. Anything outside of the brackets of the monthScroller function can't see the "moveDate" function. So... when the setTimeout runs, the scope "window" and window doesn't have a function named "moveDate" in scope. What you need to do is change the call to the following:

setTimeout(moveDate,100);

That should work for you. That way you are passing the moveDate function/object into the setTimeout.

Comments

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.