for循环中带闭包的Settimeout

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了for循环中带闭包的Settimeout相关的知识,希望对你有一定的参考价值。

  1. for (i=0;i<11;i++){
  2. //this will create 11 timers that will alert the value of 'i' at the time
  3. //the timer was created, all to fire after 1 second
  4. //to change what gets alerted, change the "(i)" portion at the end
  5. //can be used with more complex functions by adding to what's after "return"
  6. //can pass in multiple variables by adding to function(x,y,z), putting those
  7. //in proper places w/in the code, and then defining those variables at the end
  8. //ie, "(i,j,k)"
  9. setTimeout(function(x){return function(){alert(x)};}(i),1000);
  10. }

以上是关于for循环中带闭包的Settimeout的主要内容,如果未能解决你的问题,请参考以下文章

Javascript中带有For循环的innerHTML

SQL 游标 FOR 循环,模式名称中带有 / 变量

R中带有for循环的多个数据帧上的行名

虚幻引擎 4 (ue4) 中带有自定义 c++ 蓝图函数库的蓝图循环/for/while 节点

for循环中的闭包

带有闭包的 JavaScript For 循环导致 JSLint 警告