How can i make a javascript timer?

Discussion in 'JavaScript' started by bweb11, Aug 28, 2009.

  1. #1
    Hi, i'm still new to javascript but how would i make a javascript coundown timer to countdown from a time length stored in php variable <?=$pt?> (for example 3:30 in minutes and seconds) and to start counting from the click of a button. and displays it in the specified div/anchor/input/ or any other tag through the tag's id or name.

    So basiaclly a javascript timer that counts down from the time stored in <?=$pt?> on button click.
     
    bweb11, Aug 28, 2009 IP
  2. osmasters

    osmasters Well-Known Member

    Messages:
    453
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    100
    #2
    osmasters, Aug 28, 2009 IP
  3. bweb11

    bweb11 Peon

    Messages:
    19
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I knew this, but i need the code to countdown from the time stored in the variable in seconds and display each decrement.
     
    bweb11, Aug 29, 2009 IP
  4. bweb11

    bweb11 Peon

    Messages:
    19
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sorry, the last post was before visiting the link. Thanks.
     
    bweb11, Aug 29, 2009 IP
  5. bweb11

    bweb11 Peon

    Messages:
    19
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I managed to write something close to what i wanted, but it's a count-up script rather than a count down. any way if you need it:

    
    <script type="text/javascript">
    var c=0;
    var g=00;
    var t;
    var k=120;
    var ir;
    var d;
    var h=60;
    function timedCount()
    {
    document.getElementById('txt').value=d;
    c=c+1;
    ir=ir+1;
    if(c<=h)
    {
    if(c<10) {d = g+":0"+c;}
    else {d = g+":"+c;}
    }
    if(c>h)
    {
    g = g+1;
    c=0;
    }
    
    t=setTimeout("timedCount()",1000);
    
    }
    </script>
    </head>
    
    
    <form>
    <input type="text" id="txt">
    <input type="button" onclick="timedCount()">
    </form>
    
    Code (markup):
     
    bweb11, Aug 29, 2009 IP