...

View Full Version : taking hours out of this countdown



Dan13071992
02-24-2012, 11:27 PM
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?



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

Old Pedant
02-25-2012, 12:36 AM
What about dropping minutes in the final 59 seconds? You want to do that, too? I will assume so.

Change just this part:


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:


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;

Dan13071992
02-25-2012, 01:07 PM
with that all i get is:

NaN:NaN:NaN


cheers.

Dan

Philip M
02-25-2012, 02:27 PM
Old Pedant's script will not work as the time is calculated from the displayed time. var timeleft = document.getElementById('timeleft').innerHTML;


Try this:-


<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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum