0

I have the following in my page.

$(document).ready(function() {
    function setTheTimeout(){
        var t=setTimeout("alertMsg()",3000);
    }
    function alertMsg(){
        alert("Hello");
    }
    setTheTimeout();
});

I am getting an error in Firebug alertMsg() is not defined?

1
  • You don't need to assign "t" if you're never going to use it. Commented Feb 6, 2012 at 21:12

5 Answers 5

2

Change

var t=setTimeout("alertMsg()",3000);

To

var t=setTimeout(alertMsg,3000);

See the setTimeout documentation from Mozilla Developer Network. Using a string is the same as using eval, and eval is bad!

Sign up to request clarification or add additional context in comments.

3 Comments

Why can setTheTimeout access alertMsg when passed by reference, but not when passed via an eval'd string ?
@idiqual It can't, it is a scoping issue as bfavaretto said (stackoverflow.com/a/9167330/219743). I just would not recommend using a string as a parameter for the same reasons I wouldn't recommend eval.
because you're executing the reference, and eval is always bad... just pretend it doesn't exist is the best way around it!
2

That function only exists in the scope of the document.ready callback. Try this:

$(document).ready(function() {
    function setTheTimeout(){
        var t=setTimeout("alertMsg()",3000);
    }

    setTheTimeout();
});

function alertMsg(){
    alert("Hello");
}

4 Comments

Scope isn't the issue in that case, your answer won't fix his problem.
@Relic, I believe scope is the issue here. My answer does work, and is very close to his original code. Now, that doesn't mean that I'm advocating for eval. I'm just not sure if this is the right place for discussing that.
@Relic, it works, your fiddle fails because it's setup to run onDomReady. See this fork. And, in case it's not clear yet, I agree that passing the function by reference to setTimeout is by far the best option, eval is evil, everything. I just like it when the answers here explain what is going on, and I was trying to explain the scope problem. The implied eval on setTimeout was a secondary problem (not what was causing the reported error). Unfortunately, none of the answers here explained both issues satisfactorily (mine included).
@bfavaretto +1 well said... however, everything should be 'useable' by DOM ready just as a matter of practice, and if you're execution order is stable this will never be a problem.
0

Take the quotes and parenthesis off the call to alertMsg (jsFiddle).

function setTheTimeout(){
    var t=setTimeout(alertMsg,3000);
}
function alertMsg(){
    alert("Hello");
}
setTheTimeout();

Comments

0

Working example: http://jsbin.com/imovuk/edit#javascript,html

Use the following approach.

$(document).ready(function() { 
    function setTheTimeout(){ 
        var t=setTimeout(function () {alertMsg();},3000); 
    } 
    function alertMsg(){ 
        alert("Hello"); 
    } 
    setTheTimeout(); 
}); 

Eval is evil

Comments

0

It's defined a little differently than you'd expect... try this, and call back:

$(document).ready(function() {
    function setTheTimeout(){
        var t=setTimeout(alertMsg,3000);
    }
    function alertMsg(){
        alert("Hello");
    }
    setTheTimeout();
});

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.