...

View Full Version : javascript timer count by D H R



bijaybd
06-22-2012, 09:34 AM
The below code showing time left by Day:Hour:Minutes. But the hour is counting by days ways like if the day is 2 days left then hour is also 49 hours left.

i want it to be : 2 Days 1 Hour 2 Minutes left
The Day and minutes counting is working fine but not the hour.



<script language="javascript">

var j =1;

if (typeof(BackColor)=="undefined")
BackColor = "white";
if (typeof(ForeColor)=="undefined")
ForeColor= "black";

if (typeof(DisplayFormat)=="undefined")
DisplayFormat = "<span class='hour' style='margin:2px 0 0 0'>%%D%%</span><span class='min' style='margin:2px 0 0 78px'>%%H%%</span><span class='sec' style='margin:2px 0 0 149px'>%%M%%</span>";
if (typeof(CountActive)=="undefined")
CountActive = true;
if (typeof(FinishMessage)=="undefined")
FinishMessage = "";
if (typeof(CountStepper)!="number")
CountStepper = -1;
if (typeof(LeadingZero)=="undefined")
LeadingZero = true;


CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;

function calcage(secs, num1, num2) {
if(num1==3600){s = ((Math.floor(secs/num1))).toString();}
else{s = ((Math.floor(secs/num1)%num2)).toString();}
if (LeadingZero && s.length < 2)
s = "0" + s;
return "<b>" + s + "</b>";
}

function CountBack(secs,iid,j) {
if (secs < 0) {
document.getElementById(iid).innerHTML = FinishMessage;
var dealimage = "<?php echo $this->getSkinUrl('images/8.jpg') ?>";
var buynowreplace = "<?php echo $this->getSkinUrl('images/8.jpg') ?>";
document.getElementById('deal_status-'+j).innerHTML ='<img src="'+dealimage+'" alt="Rate" title="Rate"/><p>Deal closed !</p> ';
document.getElementById('scriptbuynow-'+j).style.display = "none";
document.getElementById('scriptbuynow-'+j).setAttribute('href', '#!');
document.getElementById('script_gift_box').style.display = "none";
document.getElementById('price_value-'+j).innerHTML ='<?php echo $this->__('Closed'); ?>';
return;

}
DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));

document.getElementById(iid).innerHTML = DisplayStr;
if (CountActive)
setTimeout(function(){CountBack((secs+CountStepper),iid,j)}, SetTimeOutPeriod);

}

</script>


So anybody can help me to solve this problem ?

bijaybd
07-04-2012, 09:09 PM
No HELP :(

Philip M
07-05-2012, 07:49 AM
What ghastly code! Use this instead:-




<html>
<head>
</head>


<body onload = "showTime()">

<div id = "msg"></div>

<script type = "text/javascript">

function showTime() {
today = new Date();
BigDay = new Date("December 25, 2012 12:00 UTC"); // date and time to count down to
msPerDay = 24 * 60 * 60 * 1000 ;
timeLeft = (BigDay.getTime() - today.getTime());
e_daysLeft = timeLeft / msPerDay;
daysLeft = Math.floor(e_daysLeft);
e_hrsLeft = (e_daysLeft - daysLeft)*24;
hrsLeft = Math.floor(e_hrsLeft);
minsLeft = Math.floor((e_hrsLeft - hrsLeft)*60);

if (timeLeft >0) {
document.getElementById("msg").innerHTML = "There are only<br> <h4>" + daysLeft + " days " + hrsLeft +" hours and " + minsLeft + " minutes left </h4> Until 12:00 on December 25th 2012 "
var tim = setTimeout(showTime, 1000*60); // update each minute
}
else {
document.getElementById("msg").innerHTML = "The Big Day has arrived!";
alert ("The Big Day Has Arrived!");
}

}

</script>

</body>
</html>

Haikus are easy
But sometimes they don't make sense.
Refrigerator.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum