Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-24-2012, 11:27 PM   PM User | #1
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
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
Dan13071992 is offline   Reply With Quote
Old 02-25-2012, 12:36 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 02-25-2012, 01:07 PM   PM User | #3
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
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
Dan13071992 is offline   Reply With Quote
Old 02-25-2012, 02:27 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
__________________

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.

Last edited by Philip M; 02-25-2012 at 02:35 PM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:04 PM.


Advertisement
Log in to turn off these ads.