Javascript 带有 for 循环的简单倒计时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14399834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A Simple Countdown with for loop
提问by user1599537
Hi I'm trying to create a simple countdown.
Here is my code but it's not working.
Can you help me find the problem?
嗨,我正在尝试创建一个简单的倒计时。
这是我的代码,但它不起作用。
你能帮我找出问题吗?
function count() {
for (var cc=20,cc > 0,cc--) {
document.getElementById("cco").innerHTML=cc;
}
}
count();
回答by James McLaughlin
You're using commas instead of semicolons.
您使用的是逗号而不是分号。
for (var cc=20,cc > 0,cc--)
should be
应该
for (var cc=20;cc > 0;cc--)
However, this probably won't do what you think it will, as the loop will count all the way to 0 immediately (so the user won't even see the countdown). I'm guessing you wanted something like:
但是,这可能不会像您想象的那样做,因为循环会立即一直计数到 0(因此用户甚至看不到倒计时)。我猜你想要这样的东西:
var cc = 20;
var interval = setInterval(function()
{
document.getElementById("cco").innerHTML = -- cc;
if (cc == 0)
clearInterval(interval);
}, 1000);
See setIntervaland clearIntervalfor more information.
有关更多信息,请参阅setInterval和clearInterval。
回答by Sirko
There are (at least) two mistakes.
有(至少)两个错误。
The first is syntactical: In a for loop the parameters are separated by ;and not by ,. Syntactic correct code would look like this:
第一个是语法上的:在 for 循环中,参数由;而不是由分隔,。语法正确的代码如下所示:
function count() {
for (var cc=20;cc > 0;cc--) {
document.getElementById("cco").innerHTML=cc;
}
}
Second, you do not have a countdown, but override the very same element over and over again, without any time for the user to see the result.
其次,您没有倒计时,而是一遍又一遍地覆盖相同的元素,而没有时间让用户看到结果。
A better approach would be to use setTimeout()here, which could look like this:
更好的方法是在setTimeout()这里使用,它可能如下所示:
var cc = 20;
function count() {
document.getElementById("cco").innerHTML=cc;
if ( cc > 0 ) {
setTimeout( count, 1000 );
}
}
setTimeout( count, 1000 );
The setTimeout()approach leaves some time for the browser to actually render your modifications (and for the user to see it).
该setTimeout()方法为浏览器实际呈现您的修改(以及让用户看到它)留出一些时间。
回答by VisioN
Another recursive version with setTimeout:
另一个递归版本setTimeout:
(function count(cc) {
document.getElementById("cco").innerHTML = cc;
if (cc > 0)
setTimeout(function() { count(--cc); }, 1000);
})(10);
回答by JohnJohnGa
Change ','with ';'in your for loop
更改','与';'您的for循环
function count() {
for (var cc=20;cc > 0;cc--) {
document.getElementById("cco").innerHTML=cc
}
}
count();
回答by Proggear
When making countdown make sure that start and end elements are correct.
进行倒计时时,请确保开始和结束元素是正确的。
Count up:
计数:
for (var x=0;x < 10;x++) console.log(x);
goes from 0 to 9
从0 到 9
Incorrect countdown:
倒计时错误:
for (var x=10;x > 0;x--) console.log(x);
goes from 10 to 1
从10 到 1
Correct countdown:
正确倒计时:
for (var x=10-1;x >= 0;x--) console.log(x);
goes from 9 to 0
从9 到 0

