Any idea why I have the error Uncaught ReferenceError: hello is not defined ?
function hello() {
console.log('hello ()');
setTimeout("hello ()", 1000);
}
setTimeout("hello()", 1000);
Here is a jsfiddle : http://jsfiddle.net/s9vLk/
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem is that you are passing strings to
setTimeout()which means code in the string will effectively beevaled and thus isn’t running in the scope you think it’s running in and so thehello()function isn’t found.If you change the jsfiddle options on the left from “onload” to “no wrap” it works as is because then the function will be global rather than nested inside the onload handler, but a better option is to pass a function reference to
setTimeout():(Note: no parentheses after
hello.)