最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In Javascript, what is the scope of variables used in setTimeout? - Stack Overflow

programmeradmin4浏览0评论

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?

Share Improve this question edited Apr 25, 2011 at 14:51 Donut 113k20 gold badges135 silver badges147 bronze badges asked Apr 25, 2011 at 14:50 Salman VirkSalman Virk 12.3k9 gold badges38 silver badges47 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 15

This:

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论