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.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts

    taking hours out of this countdown

    hi guys, as im still a noob at javascript, so im asking for your help, i didnt make this script, someone else did and i asked for their permission to change it to my needs, the problem is, i have it working for times that are over an hour long, but i need to also make it possible to display in just under an hour aswell, eg instead of 01:00:00 it would display 60:00 then 59:59 eg, so basically im asking, how can i take the hours out of this, without it currupting the entire JS?

    Code:
    var HAS_EXPIRED = 'Time has Expired!';
    var IS_NONE = 'None';
    function secondCountdown(s){
    	
    	if(s){
    		
    		var timeleft = document.getElementById('timeleft').innerHTML;
    		
    		if((timeleft == HAS_EXPIRED) || (timeleft == IS_NONE)) return false;
    		
    		timeleft = timeleft.replace('<font>', '');
    		timeleft = timeleft.replace('</font>', '');
    		
    		var time = timeleft.split(":");
    		
    		var secs = time[2] * 1;
    		var mins = time[1] * 1;
    		var hrs = time[0] * 1;
    		
    		secs += (mins * 60) + (hrs * 3600);
    		secs -= 1;
    		
    		if(secs <= 0){
    			
    			document.getElementById('timeleft').innerHTML = HAS_EXPIRED;
    			return false;
    			
    		} else {
    			
    			hrs = Math.floor(secs/3600);
    			secs -= (hrs * 3600);
    			
    			mins = Math.floor(secs/60);
    			secs -= (mins * 60);			
    							
    			if(hrs < 10)	hrs = '0' + hrs;
    			if(mins < 10)	mins = '0' + mins;			
    			if(secs < 10)	secs = '0' + secs;
    					
    			document.getElementById('timeleft').innerHTML = hrs + ':' + mins + ':' + secs;
    			
    		}
    		
    	}
    	
    	setTimeout('secondCountdown(true)',1000);
    	
    }
    
    bootloaderAdd('secondCountdown()');
    bootloaderOn();
    Cheers.

    Dan
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,118
    Thanks
    80
    Thanked 4,555 Times in 4,519 Posts
    What about dropping minutes in the final 59 seconds? You want to do that, too? I will assume so.

    Change just this part:
    Code:
    			if(hrs < 10)	hrs = '0' + hrs;
    			if(mins < 10)	mins = '0' + mins;			
    			if(secs < 10)	secs = '0' + secs;
    					
    			document.getElementById('timeleft').innerHTML = hrs + ':' + mins + ':' + secs;
    to this:
    Code:
        if(hrs == 0) 
        {
            hrs = ""; 
        } else {
            hrs = hrs + ":";
        }
        if ( mins == 0 ) 
        {
            mins = ""; 
         } else {
             if(mins < 10) mins = '0' + mins;			
             mins = mins + ":";
         }
         if(secs < 10)	secs = '0' + secs;
         document.getElementById('timeleft').innerHTML = hrs + mins + secs;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    with that all i get is:

    NaN:NaN:NaN


    cheers.

    Dan
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Old Pedant's script will not work as the time is calculated from the displayed time. var timeleft = document.getElementById('timeleft').innerHTML;


    Try this:-

    Code:
    <html>
    <head>
    
    <script type = "text/javascript">
    
    var timeInSecs;
    var ticker;
    var s = "";
    var dhours;  // displayed
    var dmins;
    var dsecs;
    
    function startTimer(secs){
    timeInSecs = parseInt(secs);
    ticker = setInterval("tick()",1000); 
    tick(); // to start counter display right away
    }
    
    function tick() {
    var secs = timeInSecs;
    if (secs>0) {
    timeInSecs--;
    }
    else {
    clearInterval(ticker); // stop counting at zero
    document.getElementById("countdown").innerHTML = "TIME EXPIRED";
    return false;
    }
    
    var hours= Math.floor(secs/3600);
    secs %= 3600;
    var mins = Math.floor(secs/60);
    secs %= 60;
    
    // set up hrs mins secs in display format
    
    if (hours<10) {dhours = "0" + hours + ":"}
    else {dhours = hours +":"}
    if (dhours == "00:") {dhours = ""}
    if ((mins<10) && (dhours !="")) {dmins = "0" + mins + ":"}
    else {dmins = mins+ ":"}
    if ((dhours == "") && (dmins == "00:")) {mins = ""}
    if ((dhours == "") && (dmins == "")) {s = " seconds"}
    if ((secs<10) && (dmins !="")) {dsecs = "0" + secs}
    else {dsecs = secs}
    
    var result =  dhours + dmins + dsecs + s;
    
    document.getElementById("countdown").innerHTML = result;
    }
    
    </script>
    
    </head>
    
    <body onload = "startTimer(5 * 60)">
    
    <span id="countdown" style="font-weight: bold;"></span>
    
    </body>
    </html>
    If it is not obvious, the start is number of minutes x 60 (seconds). So 2 hours would be 120 minutes.


    And so for the moment the Great Britain ladies' hockey team is down to ten men. - Commentator BBC Sport
    Last edited by Philip M; 02-25-2012 at 03:35 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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