I have this code
window.onload = function() {
function foo() {
alert("test");
}
setInterval("foo()",500)
}
Which returns undefined…When i use it outside the window.onload it works. Can anyone explain me why?
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.
Using a string command in
setInterval()will try to look for the function in the global (window) scope, but since the function is defined in a local scope, it won’t be found. You should pass the function itself tosetInterval()instead.