Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
110
rated 0 times [  116] [ 6]  / answers: 1 / hits: 17302  / 15 Years ago, fri, january 29, 2010, 12:00:00

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?


More From » setinterval

 Answers
24

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 to setInterval() instead.



window.onload = function() {            
function foo() {
alert(test);
}
setInterval(foo, 500);
}

[#97715] Tuesday, January 26, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jackelyn

Total Points: 303
Total Questions: 103
Total Answers: 102

Location: Turks and Caicos Islands
Member since Sun, Mar 7, 2021
3 Years ago
jackelyn questions
Thu, Apr 8, 21, 00:00, 3 Years ago
Sun, Feb 28, 21, 00:00, 3 Years ago
Mon, May 25, 20, 00:00, 4 Years ago
Thu, Apr 30, 20, 00:00, 4 Years ago
;