2010-06-15 93 views
6

我试图在控制台(Firefox/Firefly,Mac)中记录一个值的更改。简单的JavaScript控制台日志(FireFox)

if(count < 1000) 
{ 
    count = count+1; 
    console.log(count); 
    setTimeout("startProgress", 1000); 
} 

这只返回值1,之后停止。

我做错了什么或有什么其他影响呢?

+10

您不应该将字符串传递给'setTimeout'。 – SLaks 2010-06-15 21:18:03

+0

@SLaks:+1。如果可以的话,会是+(> 1)。 – 2010-06-15 21:20:27

+0

@SLaks:http://www.w3schools.com/js/js_timing.asp 我错了吗? – 2010-06-15 21:22:15

回答

10

您没有循环。只有一个条件语句。使用while

var count = 1; 
while(count < 1000) { 
     count = count+1; 
     console.log(count); 
     setTimeout("startProgress", 1000); // you really want to do this 1000 times? 
} 

更好:

var count = 1; 
setTimeout(startProgress,1000); // I'm guessing this is where you want this 
while(count < 1000) { 
    console.log(count++); 
} 
+0

感谢您的快速响应,肯! – 2010-06-15 21:18:14

+0

这样做的窍门,但暂停不工作......任何想法? – 2010-06-15 21:19:23

+0

我猜你想在循环之前将它关闭?不知道你想要完成什么,因为我不知道startProgress应该是什么。我假定你的意思是做一个函数调用? – 2010-06-15 21:21:21

1

我认为你正在寻找while循环有:

var count = 0; 
while(count < 1000) { 
    count++; 
    console.log(count); 
    setTimeout("startProgress", 1000); 
} 
1

至于其他的答案表明,if VS while是你的问题。然而,这是一个更好的办法是使用setInterval(),像这样:

setinterval(startProcess, 1000); 

这不会在1000个呼叫阻止,但我假设你只是这样做,用于测试目的的时刻。如果您确实需要停止这样做,您可以使用clearInterval(),如下所示:

var interval = setinterval(startProcess, 1000); 
//later... 
clearInterval(interval); 
+0

我不确定他想要做什么,但+1。 – 2010-06-15 21:54:40