Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 45
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need A Repeating Countdown Timer

    Hi guys. I'm making a webpage for a school project and I need help modifying this timer here:

    http://web.mac.com/rkuhnhenn/Countdo.../Style_3b.html

    I want it to countdown from 48 hours, and as soon as it counts down, I need it to reset. I need an endless countdown of 48 hours.

    How can I go about doing that?


    Thanks guys, your help is highly appreciated.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Voila.

    Code:
    <script type = "text/javascript">
    
    var timeInSecs;
    var ticker;
    
    function startTimer(secs) {
    timeInSecs = parseInt(secs);
    ticker = setInterval("tick()", 1000); 
    }
    
    function tick( ) {
    var secs = timeInSecs;
    if (secs > 0) {
    timeInSecs--; 
    }
    else {
    clearInterval(ticker);
    startTimer(172800);  // start again
    }
    
    var hours= Math.floor(secs/3600);
    secs %= 3600;
    var mins = Math.floor(secs/60);
    secs %= 60;
    var pretty = ( (hours < 10 ) ? "0" : "" ) + hours + ":" + ( (mins < 10) ? "0" : "" ) + mins + ":" + ( (secs < 10) ? "0" : "" ) + secs;
    document.getElementById("countdown").innerHTML = pretty;
    }
    
    startTimer(172800);  // 48 hours in seconds
    
    </script>
    
    <span id="countdown" style="font-weight: bold;"></span>

    “I am so clever that sometimes I don't understand a single word of what I am saying.” - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)
    Last edited by Philip M; 05-27-2009 at 08:24 AM. Reason: Spotted error

  • #3
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hey i really badly need this same exact thing but counting down 18 minutes. so i need it to start at 18 minutes and reset every time it reaches 0...but can you also make one to go with it that counts up +1 every 18 minutes starting at 0? it's for a suicide help/prevention website so it's very important

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Just use
    startTimer(18*60); // 18 minutes in seconds
    in the two places where he has
    startTimer(172800);

    ***********

    What do you mean "count up by +1"???

    You mean increase 18 minutes to 19? And then to 20? Or what?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    What is so special about 18 minutes? If you explained your purpose more clearly people would be better able to assist you.

  • #6
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    alright by plus 1 i mean that belo the 18 minute count down timer there is another counter that starts at 0 and increases by 1 every 18 minutes. so it counts how many times the 18 minute count down timer has reached 0.

  • #7
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry to complicate this but i need it to work in a custom html editor as used at webs.com for editing.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    No idea why the custom HTML editor would impact JS code.

    But the answer to your count up is simple:
    Code:
    var upCount = -1;
    function startTimer(secs) 
    {
        document.getElementById("UpCounter").innerHTML = "count is now " + (++upCount);
        timeInSecs = parseInt(secs);
        ticker = setInterval("tick()", 1000); 
    }
    ...
    and then someplace on your page, where you want it, you have
    <div id="UpCounter"></div>
    *OR*
    <span id="UpCounter"></span>

  • #9
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well i don't know wats happening but i put the two different things into seperate html's an this is what showed up on the webpage...

    var upCount = -1; function startTimer(secs) { document.getElementById("UpCounter").innerHTML = "count is now " + (++upCount); timeInSecs = parseInt(secs); ticker = setInterval("tick()", 1000); } ... and then someplace on your page, where you want it, you have

    *OR* var timeInSecs; var ticker; function startTimer(secs) { timeInSecs = parseInt(secs); ticker = setInterval("tick()", 1000); } function tick( ) { var secs = timeInSecs; if (secs > 0) { timeInSecs--; } else { clearInterval(ticker); startTimer(1080); // start again } var hours= Math.floor(secs/3600); secs %= 3600; var mins = Math.floor(secs/60); secs %= 60; var pretty = ( (hours < 10 ) ? "0" : "" ) + hours + ":" + ( (mins < 10) ? "0" : "" ) + mins + ":" + ( (secs < 10) ? "0" : "" ) + secs; document.getElementById("countdown").innerHTML = pretty; } startTimer(1080); // 18 minutes in seconds

  • #10
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mhourihan View Post
    well i don't know wats happening but i put the two different things into seperate html's an this is what showed up on the webpage...

    var upCount = -1; function startTimer(secs) { document.getElementById("UpCounter").innerHTML = "count is now " + (++upCount); timeInSecs = parseInt(secs); ticker = setInterval("tick()", 1000); } ... and then someplace on your page, where you want it, you have

    *OR* var timeInSecs; var ticker; function startTimer(secs) { timeInSecs = parseInt(secs); ticker = setInterval("tick()", 1000); } function tick( ) { var secs = timeInSecs; if (secs > 0) { timeInSecs--; } else { clearInterval(ticker); startTimer(1080); // start again } var hours= Math.floor(secs/3600); secs %= 3600; var mins = Math.floor(secs/60); secs %= 60; var pretty = ( (hours < 10 ) ? "0" : "" ) + hours + ":" + ( (mins < 10) ? "0" : "" ) + mins + ":" + ( (secs < 10) ? "0" : "" ) + secs; document.getElementById("countdown").innerHTML = pretty; } startTimer(1080); // 18 minutes in seconds
    alrght right after i posted i reread the second code an realized it wasn't all code so that's solved.
    but the rest isn't solv

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    I gather that English is not your native language. Sorry if I wasn't clear. I'll try again.

    The part in blue here is just MY COMMENTS TO YOU

    The part in green here goes SOMEPLACE on your web page and should *NOT* be part of the javascript code.

    Also, you can only use ONE of the two choices in green. You can *NOT* use both of them.


    and then someplace on your page, where you want it, you have
    <div id="UpCounter"></div>
    *OR*
    <span id="UpCounter"></span>


    And then ONLY this code is JavaScript, and it *REPLACES* the function startTimer() as you showed that function in your post. It should *NOT* be put in a separate page or file or whatever. It is a *change* to your existing code.
    Code:
    var upCount = -1;
    function startTimer(secs) 
    {
        document.getElementById("UpCounter").innerHTML = "count is now " + (++upCount);
        timeInSecs = parseInt(secs);
        ticker = setInterval("tick()", 1000); 
    }

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Oh, sorry...it's a change to the code that PhilipM posted, that is.

  • #13
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    alright english is my native language but my computer lags and misses keys and i was talking to someone while typing, and when i copied the code i was also talking then so i didn't read it and then realized afterwards that i copied the wrong things. but the original code at the top of the page wheni use it, the webpage just shows the code, even whe i put it into an html editor..

  • #14
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok so this change, am i replacing the entire original code? or am i adding it to the bottom?

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Look, I don't know what HTML editor you are using, but it sounds like it is messing you over more than it is helping you.

    Shut it down, temporarily at least, and just open up the page in a text editor, such as NOTEPAD.

    As far as the code goes: You are taking the code PhilipM posted and replacing *ONLY* the part that I showed you. ONE FUNCTION. Replacing. Not adding.

    This is, of course, in addition to changing that number from whatever it was to 18*60 for 18 minutes worth of seconds.


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •