1

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();
PaulStock
  • 11,053
  • 9
  • 49
  • 52
user1599537
  • 1,269
  • 3
  • 10
  • 10
  • What is the problem? from what I see it can be either you lack a sleep of some sort or you have no element with `id="coo"`, or the `,` instead of `;` typo in the for loop. – Ofir Farchy Jan 18 '13 at 13:21
  • The problem is his comma's instead of semi-colons in the for argument. Otherwise you are right, in this manner (without a 'sleep/pauze' like a recursive setInterval(f,t), the element cco will only have 0 visible. – GitaarLAB Jan 18 '13 at 13:22
  • 3
    @OfirFarchy, what JavaScript version are you using that it has sleep? ;) – epascarello Jan 18 '13 at 13:23
  • @epascarello, correct :) language mixup, fixed the comment – Ofir Farchy Jan 18 '13 at 13:25

5 Answers5

11

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:

var cc = 20;

var interval = setInterval(function()
{
    document.getElementById("cco").innerHTML = -- cc;

    if (cc == 0)
        clearInterval(interval);

}, 1000);

See setInterval and clearInterval for more information.

James M
  • 18,506
  • 3
  • 48
  • 56
  • 1
    Looks like you need to add a `clearInterval` in case `cc` is `0`. – VisioN Jan 18 '13 at 13:23
  • Or you could use a recursive `setTimeout` that stops setting when `cc` is `0`. – mellamokb Jan 18 '13 at 13:23
  • 1
    Yes, you're right - admittedly, I didn't test the code. Thanks! – James M Jan 18 '13 at 13:24
  • 1
    @mellamokb Although in this situation it probably doesn't matter, I find `setInterval` is more precise with the interval than a recursive `setTimeout`. There's a good description of the difference [here](http://stackoverflow.com/a/731625/712294). – James M Jan 18 '13 at 13:29
4

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:

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:

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).

Sirko
  • 72,589
  • 19
  • 149
  • 183
  • +1 I prefer `setTimeout`. It's like using a [Thorium nuclear reactor](http://en.wikipedia.org/wiki/Thorium-based_nuclear_power) instead of a Uranium one. – mellamokb Jan 18 '13 at 13:26
2

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

Incorrect countdown:

for (var x=10;x > 0;x--) console.log(x);

goes from 10 to 1

Correct countdown:

for (var x=10-1;x >= 0;x--) console.log(x);

goes from 9 to 0

Proggear
  • 662
  • 6
  • 10
1

Change ',' with ';' in your for loop

function count() {
  for (var cc=20;cc > 0;cc--) {
    document.getElementById("cco").innerHTML=cc
  }
}
count();
JohnJohnGa
  • 15,446
  • 19
  • 62
  • 87
1

Another recursive version with setTimeout:

(function count(cc) {
  document.getElementById("cco").innerHTML = cc;
  if (cc > 0)
    setTimeout(function() { count(--cc); }, 1000);
})(10);

DEMO: http://jsfiddle.net/DgWgx/

VisioN
  • 143,310
  • 32
  • 282
  • 281