我試圖在控制檯(Firefox/Firefly,Mac)中記錄一個值的更改。簡單的JavaScript控制檯日誌(FireFox)
if(count < 1000)
{
count = count+1;
console.log(count);
setTimeout("startProgress", 1000);
}
這隻返回值1,之後停止。
我做錯了什麼或有什麼其他影響呢?
我試圖在控制檯(Firefox/Firefly,Mac)中記錄一個值的更改。簡單的JavaScript控制檯日誌(FireFox)
if(count < 1000)
{
count = count+1;
console.log(count);
setTimeout("startProgress", 1000);
}
這隻返回值1,之後停止。
我做錯了什麼或有什麼其他影響呢?
您沒有循環。只有一個條件語句。使用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++);
}
感謝您的快速響應,肯! – 2010-06-15 21:18:14
這樣做的竅門,但暫停不工作......任何想法? – 2010-06-15 21:19:23
我猜你想在循環之前將它關閉?不知道你想要完成什麼,因爲我不知道startProgress應該是什麼。我假定你的意思是做一個函數調用? – 2010-06-15 21:21:21
我認爲你正在尋找while
循環有:
var count = 0;
while(count < 1000) {
count++;
console.log(count);
setTimeout("startProgress", 1000);
}
至於其他的答案表明,if
VS while
是你的問題。然而,這是一個更好的辦法是使用setInterval()
,像這樣:
setinterval(startProcess, 1000);
這不會在1000個呼叫阻止,但我假設你只是這樣做,用於測試目的的時刻。如果您確實需要停止這樣做,您可以使用clearInterval()
,如下所示:
var interval = setinterval(startProcess, 1000);
//later...
clearInterval(interval);
我不確定他想要做什麼,但+1。 – 2010-06-15 21:54:40
您不應該將字符串傳遞給'setTimeout'。 – SLaks 2010-06-15 21:18:03
@SLaks:+1。如果可以的話,會是+(> 1)。 – 2010-06-15 21:20:27
@SLaks:http://www.w3schools.com/js/js_timing.asp 我錯了嗎? – 2010-06-15 21:22:15