In javascript , we usually use setInterval like this:
window.onload = function(){
setInterval('say()',1000);
}
function say(){
console.log(haha);
}
In function say() , if it has parameter , how and correctly pass parameter use setInterval.
First , I use it like this:
window.onload = function(){
var hi = 'hello';
setInterval('say("hi")',1000);
}
function say(haha){
console.log(haha);
}
But the result tells me 'variable hi no defined' .
Then I use this :
var hi = 'hello';//it becomes to global
window.onload = function(){
setInterval('say("hi")',1000);
}
function say(haha){
console.log(haha);
}
Then it can execute correctly . I don't know why.
finaly , If I want use local variable as function's parameter in setInterval , we can do it like this:
window.onload = function(){
var hi = 'hello';//this is local variable
setInterval('say("' + hi + '")',1000);
}
function say(haha){
console.log(haha);
}