I am using the following code in a function:
setTimeout("doSomething(var1)",10000);
But, I also have var1
available as global variable. After 10000 milliseconds, will it call the local var1
or the global var1
?
I am using the following code in a function:
setTimeout("doSomething(var1)",10000);
But, I also have var1
available as global variable. After 10000 milliseconds, will it call the local var1
or the global var1
?
2 Answers
Reset to default 15This:
setTimeout('doSomething(var1)', 10000);
will pass the global variable var1
,
And this:
setTimeout(function() { doSomething(var1); }, 10000);
will pass the local variable var1
.
Live demo: http://jsfiddle/simevidas/EQMaz/
It will pass the the global variable named var1
.