2012-03-26 37 views
2

我想暂停1 second为每次循环的时间内睡觉,它通常是很容易做到的其他情况类似的停顿,但随着循环工作时,它似乎越来越难以:无法管理到循环

for (var i=0 ; i < 10 ; i++) { 
    document.write (i + "<br>"); 
    // I want to wait 1 second here 
} 

这是我十万一例失败的尝试:

function writeMsg (index) { 
    document.write (index + "<br>"); 
} 

for (var i=0 ; i < 10 ; i++) { 
    setTimeout (writeMsg(i), 1000); 
} 

如何得到这个工作任何想法?

+3

[在一个for循环setTimeout和传递i设定为值]的可能重复(http://stackoverflow.com/questions/5226285/settimeout-in-a-for-loop-and-pass-i - 价值)和http://stackoverflow.com/questions/6564814/passing-argument-to-settimeout-in-a-for-loop和http://stackoverflow.com/questions/5986588/calling-settimeout- function-within-a-loop和http://stackoverflow.com/questions/6425062/passing-functions-to-settimeout-in-a-loop-always-the-last-value和http:// stackoverflow。com/questions/3445855/javascript-how-to-pass-different-object-to-settimeout-handlers-created-in-a-loo和其他约100k其他 – 2012-03-26 21:51:39

+0

其实他的主要问题是他调用'writeMsg(i )'立即,而不是传递一个函数setTimeout ... – ThiefMaster 2012-03-26 21:56:07

+2

@MДΓΓБДLL令人惊讶的是,虽然没有很多那些得到正确的答案:s ... – sg3s 2012-03-26 22:24:13

回答

9

此功能更像是一个正常的循环,而不是

你需要考虑到,一个用于获取3个参数其间的分号。

  1. 开始之前(即var i=0你定义一个变量)
  2. 再次运行代码前的状态(即i < 10而我是10岁以下)
  3. 的动作,每次它完成再一次的代码(i++添加一个与i)

代码

(function() { 
    // Define a variable 
    var i = 0, 
     action = function() { 
      // Condition to run again 
      if (i < 10) { 
       document.write(i + "<br>"); 

       // Add one to i 
       i++; 
       setTimeout(action, 1000); 
      } 
     }; 

    setTimeout(action, 1000); 
})(); 

下面是该代码演示其工作一的jsfiddle: http://jsfiddle.net/sg3s/n9BNQ/

+0

我猜测,如果围绕新的setTimeout而不是整个函数,我的代码可以简化。 – sg3s 2012-03-26 22:11:22

+0

它不是一个真正的睡眠,但它的好处 – themis 2014-12-04 05:13:14

4

您将函数调用的返回值传递给setTimeout而不是函数。试试下面的代码:

for (var i = 0; i < 10; i++) { 
    (function(i) { 
     setTimeout(function() { 
      writeMsg(i); 
     }, 1000*i); 
    })(i); 
} 

如果你想知道为什么调用包装的匿名函数中:如果没有这个功能,每个setTimeout的回调将收到相同的i所以当回调火将永远是10。匿名函数在里面创建一个新的i,它没有连接到循环变量。

+0

他们同时打印所有(铬) – ajax333221 2012-03-26 21:45:08

+0

是啊,我忘了'1000 *我'而不是'1000'。您可能已复制初始版本。 – ThiefMaster 2012-03-26 21:45:42

+0

每次打印'10'。但我现在可以尝试使用 – ajax333221 2012-03-26 21:47:02

1

传统的功能在循环问题。一个典型的解决方案:

function createCallback(i) { 
    return function() { 
     writeMsg(i); 
    }; 
} 

function writeMsg (index) { 
    document.write (index + "<br>"); 
} 

for (var i=0 ; i < 10 ; i++) { 
    setTimeout (createCallback(i), 1000*i); 
} 
+0

你立即打电话给该功能... – ThiefMaster 2012-03-26 21:45:15

+0

D'oh,糟pa了parens。这就是为什么我喜欢独立的回调生成器 - 更容易编写,更易于阅读。 – 2012-03-26 21:46:13

+0

糟糕的做法是,你启动多个超时,并打破功能正常,因为你不能改变我影响其他潜在的代码循环。 – sg3s 2012-03-26 22:08:36

1

的10个超时都基于这样的setTimeout()被调用的时候。所以,他们都在同一时间触发。

for (var i=0; i < 10; i++) { 
    (function(idx){ 
     setTimeout(function(){ 
      document.write(idx+"<br/>"); 
     },1000*idx); 
    })(i); 
}; 
+0

不好的方法来做到这一点,你启动多个超时和功能打破正常,因为你不能改变我影响其他潜在的代码循环。 – sg3s 2012-03-26 22:06:36

0

尝试这肯定会帮助谁所有人都认为如何使它工作等在内的For循环财产... 尝试在这个网址http://www.shopjustice.com/the-collections/C-10329此代码。

var var2; 
var tmp; 
var evt; 
var i=0; 
var res = document.getElementsByClassName('mar-plp-filter-content nav nav--stacked')[0].children.length; 
tmp = document.getElementsByClassName('mar-plp-filter-content nav nav--stacked')[0].children; 
function myfunc() 
{ 
if(i<res) 
{ 
var2 = tmp[i].getElementsByTagName("span")[0].getElementsByClassName("inverted")[0]; 
// alert(var2.innerHTML); 
var evObj = document.createEvent('MouseEvents'); 
evObj.initEvent('mouseover', true, false); 
var2.dispatchEvent(evObj); 
var2.style.backgroundColor="GREEN"; 
i++; 
setTimeout(myfunc,3000); 
} 
}; 
setTimeout(myfunc,3000);