...

View Full Version : I need a zero added for my Javascript countdown. so 9 looks like 09



eclipsemedia
12-01-2009, 04:22 PM
Hey Everyone,

I'm happy to have joined this forum. I have a javascript countdown but the digits countdown like 14 13 12 11 10 9 8 7 ... and I want it to look like 14 13 12 11 10 09 08 07.... I need this done for Days Hours Minutes and Seconds.

Here is the code. Thank you in advanced for your help.




<script type="text/javascript">
function cd() {
var now = <?php echo $now; ?>;
var target = <?php echo $target; ?>;
var horizvert = '<?php echo $horizvert; ?>';
var daytext = '<?php echo $daytext; ?>';
var daystext = '<?php echo $daystext; ?>';
var hourtext = '<?php echo $hourtext; ?>';
var hourstext = '<?php echo $hourstext; ?>';
var minutetext = '<?php echo $minutetext; ?>';
var minutestext = '<?php echo $minutestext; ?>';
var secondtext = '<?php echo $secondtext; ?>';
var secondstext = '<?php echo $secondstext; ?>';
var whatnow = '<?php echo $whatnow; ?>';
var redirect = '<?php echo $redirect; ?>';

timediff = target - now;

var daysleft = 0;
var hoursleft = 0;
var minutesleft = 0;
var secondsleft = timediff;

if (timediff >= 60) {
secondsleft = timediff % 60;
minutesleft = (timediff - secondsleft) / 60;
}

if (minutesleft >= 60) {
timediff = minutesleft;
minutesleft = timediff % 60;
hoursleft = (timediff - minutesleft) / 60;
}

if (hoursleft >= 24) {
timediff = hoursleft;
hoursleft = timediff % 24;
daysleft = (timediff - hoursleft) / 24;
}

var gmctime = document.getElementById("gmctime");
var gmctimetext = '';

var gmccountdown_timer = setInterval(gmcTimer, 1000);

function gmcUpdateDivHorizontal() {
gmctimetext = '';
gmctimetext += (daysleft) ? daysleft + (daysleft==1 ? ' '+daytext+' ' : ' '+daystext+' ') : '';
gmctimetext += (hoursleft || daysleft) ? hoursleft + (hoursleft==1 ? ' '+hourtext+' ' : ' '+hourstext+' ') : '';
gmctimetext += (minutesleft || hoursleft || daysleft) ? minutesleft + (minutesleft==1 ? ' '+minutetext+' ' : ' '+minutestext+' ') : '';
gmctimetext += secondsleft + (secondsleft==1 ? ' '+secondtext+' ' : ' '+secondstext+' ');
gmctime.innerHTML = gmctimetext;
}

function gmcUpdateDivVertical() {
gmctimetext = '';
gmctimetext += (daysleft) ? daysleft + (daysleft==1 ? ' '+daytext+'<br />' : ' '+daystext+'<br />') : '';
gmctimetext += (hoursleft || daysleft) ? hoursleft + (hoursleft==1 ? ' '+hourtext+'<br />' : ' '+hourstext+'<br />') : '';
gmctimetext += (minutesleft || hoursleft || daysleft) ? minutesleft + (minutesleft==1 ? ' '+minutetext+'<br />' : ' '+minutestext+'<br />') : '';
gmctimetext += secondsleft + (secondsleft==1 ? ' '+secondtext+'<br />' : ' '+secondstext+'<br />');
gmctime.innerHTML = gmctimetext;
}

function gmcTimer() {
if (secondsleft == 0 && minutesleft == 0 && hoursleft == 0 && daysleft ==0) {
clearInterval(gmccountdown_timer);
if (whatnow == 'text') {
document.getElementById('gmcpre').style.display = 'none';
document.getElementById('datetime').style.display = 'none';
document.getElementById('gmcpost').style.display = 'none';
document.getElementById('gmcafter').style.display = 'block';
} else {
window.location = redirect;
}
return;
}

if (secondsleft > 0) secondsleft--;
else {
secondsleft = (minutesleft || hoursleft || daysleft) ? 59 : 0;
if (minutesleft > 0) minutesleft--;
else {
minutesleft = (hoursleft || daysleft) ? 59 : 0;
if (hoursleft > 0) hoursleft--;
else {
hoursleft = (daysleft) ? 23 : 0;
if (daysleft) daysleft--;
}
}
}

if (horizvert == 'Horizontal') {
gmcUpdateDivHorizontal();
} else {
gmcUpdateDivVertical();
}
}
}
window.onload = cd;
</script>

Philip M
12-01-2009, 04:36 PM
The way your script is written means that it cannot readily be modified as you wish. Here is an alternative, which if you insist on using your own script will show you the way to go about modifying it.



<script type = "text/javascript">

var timeInSecs;
var ticker;

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
//startTimer(60 * 60 *24 * 5); // and start again if required
}

var days = Math.floor(secs/86400);
secs %= 86400;
var hours= Math.floor(secs/3600);
secs %= 3600;
var mins = Math.floor(secs/60);
secs %= 60;

var result = ((hours < 10 ) ? "0" : "" ) + hours + ":" + ( (mins < 10) ? "0" : "" ) + mins
+ ":" + ( (secs < 10) ? "0" : "" ) + secs;
result = days + " Days: " + result;
document.getElementById("countdown").innerHTML = result;
}

</script>

</head>

<body onload = "startTimer(60 * 60 * 24 * 5)">

<span id="countdown" style="font-weight: bold;"></span>

At its simplest:-


if (days <10) {days = "0" + days}but of course the variable days is then a string, not a number, so is no longer available for further calculations.

Or you could do thus:-


if (hoursleft >= 24) {
timediff = hoursleft;
hoursleft = timediff % 24;
daysleft = (timediff - hoursleft) / 24;
daysleftdisplay = daysleft;
if (daysleftdisplay <10) {daysleftdisplay = "0" + days)
}and use daysleftdisplay and so on in the functions which generate the gmctimetext


"Good judgment comes from experience and experience comes from bad judgment."

eclipsemedia
12-09-2009, 07:36 PM
Thank you for your reply. So there is no way the top script can be modified and copied and pasted with out doing other mods? I don't know enough of Javascript to rewrite the code I posted. :(

Philip M
12-09-2009, 07:59 PM
Thank you for your reply. So there is no way the top script can be modified and copied and pasted with out doing other mods? I don't know enough of Javascript to rewrite the code I posted. :(

See Post #2. The way your script is written means that it cannot readily be modified as you wish.

Presumably you would not attempt to service or repair a machine or appliance without any experience or understanding of how it worked.

eclipsemedia
12-09-2009, 08:13 PM
Exactly. Thats why I found another machine :) with the leading zeros as needed. Thank you again:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum