views:

202

answers:

6

This is my code function countdown(integer) { for i = integer, 0, -1 do { document.write(i); } } What I am trying to do is have a loop do what I want it to do, and what I want it to do is..
for i = integer, 0, -1 do
i = The variable of the current loop
integer = Starting the loop at the integer called by the user
0 = The ending of the loop
-1 = The increment of the loop

The above is solved, the problem now is it is only showing "->1<-".

function countdown(integer)
{
    for( var i = integer; i > 0 ; i-- ) 
    {
        setTimeout(document.getElementById("cds").value="->"+i+"<-",1000);
    }
}
+11  A: 
for( var i = integer; i > 0 ; i-- ) 
{
    document.write(i);
}
psychotik
+2  A: 

A for loop executes right away (without waiting for the setTimeout to trigger) and hence your code will set integer number of timers at the same time (in a gap of microseconds), and only the last one (where i is 1), will be visible to the user. You should do it as:

var counter = integer;
setTimeout("update();", 1000);
function update()
{
    document.getElementById("cds").value= "->" + counter + "<-";
    if(--counter == 0)
        return;
    setTimeout("update();", 1000);
}
Amarghosh
+1  A: 

Faster (I checked it) and easier to read...

var i = 100;
while(i--) {
   document.write(i);
}
Emrah GOZCU
I am not saying it isn't faster, I am saying it's a hack that's not obvious to understand. If you are the only one who'll see this code, well go for it. But if there's going to be someone else maintaining it, why don't make it clear?
quantumSoup
@Aircule: It is not easy to understand for beginners, but once they understand, i think it is easy to read for next time they look at the code. I don't say it is best way to do so, but for me it is prefered.
Emrah GOZCU
What if I decide that now I just want to go from 100 to 2?
quantumSoup
@Aircule: when I see that you're disagree, i post the wrong comment than i deleted it :) sorry. I posted another comment a minute ago.
Emrah GOZCU
I would simply change the initial value of i to 2. Kidding :) Yes, I am getting your point, I don't say you are wrong and i am the right one. I've just writing this answer and i saw some answers and just decided to give my opinion about it.
Emrah GOZCU
aha! @Aircule - of course that can be used up to 2, like this `while(i-->2) { document.write(i);}​` let's play with this demo http://jsfiddle.net/XbX9S/
Reigel
@Reigel Touché :)
quantumSoup
@Aircule Votre accueil? :)
Reigel
+1  A: 

Hello You can use below script.

for( var i = integer; i > 0 ; i-- ) { document.write(i); }

Thanks.

Yunus Malek
How is it different from psychotik's answer? If it is not, what value does it add?
Amarghosh
+3  A: 

according to your update, I guess you do not need the for loop, you need this,

demo

function countdown(integer) {
   var time = setInterval(function(){
        document.getElementById("cds").value="->"+(integer--)+"<-"
        if (integer == 0) clearInterval(time);
    },1000);
}​
Reigel
Thank you so much. :D
Anonymous the Great
@Anonymous the Great: As I've said, this is not fair! psychotik gave the right answer for this. You changed the question!
Emrah GOZCU
A: 

I'm guessing you want to show a number countdown in a specified input box. Let me give it a shot.

<html>
  <head>
    <script type="text/javascript">
    function countdown(count) {
      document.getElementById("cds").value = count;
      if (count > 0) {
        setTimeout("countdown(" + (count - 1) + ")", 1000);
      }
    }
    </script>
  </head>
  <body onload="countdown(10)">
    <input type="text" id="cds" value="0" />
  </body>
</html>

The trick is to put the recursion call in the setTimeout function and surround that with the condition that stops the countdown when 0 is reached.

Symen Timmermans