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
  • 10,663
  • 9
  • 46
  • 52
user1599537
  • 1,159
  • 3
  • 9
  • 9
  • 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

10

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 McLaughlin
  • 18,774
  • 3
  • 47
  • 56
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
  • 69,531
  • 19
  • 142
  • 174
  • +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
1

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

function count() {
  for (var cc=20;cc > 0;cc--) {
    document.getElementById("cco").innerHTML=cc
  }
}
count();
JohnJohnGa
  • 15,076
  • 17
  • 60
  • 86
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
  • 138,460
  • 30
  • 271
  • 271
1

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
  • 598
  • 4
  • 9