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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2008
    Posts
    103
    Thanks
    4
    Thanked 0 Times in 0 Posts

    javascript timer count by D H R

    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.


    Code:
                <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 ?

  • #2
    Regular Coder
    Join Date
    Nov 2008
    Posts
    103
    Thanks
    4
    Thanked 0 Times in 0 Posts
    No HELP

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    What ghastly code! Use this instead:-


    Code:
    <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.

    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
    •