www.webdeveloper.com
Results 1 to 3 of 3

Thread: Countdown clock

  1. #1
    Join Date
    Feb 2008
    Posts
    10

    Countdown clock

    Hi all,

    I'm having an issue with the countdown clock. I use the code at http://www.hashemian.com/tools/javascript-countdown.htm

    In that site, it seems that every thing is ok. However, in my site it's not working properly...

    Here is my ColdFusion page (you can replace with another language, I just use the now() function of CFusion, just because I need to get the Time on server, not on Client)

    Code:
    <cfoutput>
    <html>
    
    <script language="JavaScript">
    TargetDate = "#DateFormat('2-29-2008', 'mm/dd/yyyy')#" + " " + "#TimeFormat('16:00', 'h:MM:ss tt')#";
    Now = "#DateFormat(now(), 'mm/dd/yyyy')#" + " " + "#TimeFormat(now(), 'h:MM:ss tt')#";
    CountActive = true;
    CountStepper = -1;
    LeadingZero = true;
    DisplayFormat = "%%D%% D, %%H%% H, %%M%% M, %%S%% S.";
    FinishMessage = "This item has expired";
    
    </script>
    
    
    <body>
    	<script language="javascript" type="text/javascript" src="scripts/countdown.js"></script>
    </body>
    </html>
    </cfoutput>
    And the JavaScript code is as following

    Code:
    /*
    Author: Robert Hashemian
    http://www.hashemian.com/
    
    You can use this code in any manner so long as the author's
    name, Web address and this disclaimer is kept intact.
    ********************************************************
    Usage Sample:
    
    <script language="JavaScript">
    TargetDate = "12/31/2020 5:00 AM";
    BackColor = "palegreen";
    ForeColor = "navy";
    CountActive = true;
    CountStepper = -1;
    LeadingZero = true;
    DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes.";
    FinishMessage = "It is finally here!";
    </script>
    <script language="JavaScript" src="http://scripts.hashemian.com/js/countdown.js"></script>
    */
    
    function calcage(secs, num1, num2) {
      s = ((Math.floor(secs/num1))%num2).toString();
      if (LeadingZero && s.length < 2)
        s = "0" + s;
      return "<b>" + s + "</b>";
    }
    
    function CountBack(secs) {
      if (secs < 0) {
        document.getElementById("cntdwn").innerHTML = FinishMessage;
        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("cntdwn").innerHTML = DisplayStr;
      if (CountActive)
        setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
    }
    
    function putspan(backcolor, forecolor) {
     document.write("<span id='cntdwn' style='color:" + forecolor + "'></span>");
    }
    
    if (typeof(BackColor)=="undefined")
      BackColor = "white";
    if (typeof(ForeColor)=="undefined")
      ForeColor= "black";
    if (typeof(TargetDate)=="undefined")
      TargetDate = "12/31/2020 5:00 AM";
    if (typeof(DisplayFormat)=="undefined")
      DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes.";
    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;
    putspan(BackColor, ForeColor);
    var dthen = new Date(TargetDate);
    var dnow = new Date(Now);
    if(CountStepper>0)
      ddiff = new Date(dnow-dthen);
    else
      ddiff = new Date(dthen-dnow);
    gsecs = Math.floor(ddiff.valueOf()/1000);
    CountBack(gsecs);

    If we wait for about 2 minutes then press F5. The time will be different...For example, when I open the page, the time is ... 10 minutes 30sec, after 2 minutes, the time now is 8 minutes 30 sec...at this time I press F5...The page is loaded again and the time will be 8 minutes 49sec... or sometimes it will be 7 minutes 30 sec...

    I just guess the clock is running faster than the real time...but ...sometimes it's faster and sometimes it's slower....so I can't understand...

    Do you have any ideas? Thanks very much

  2. #2
    Join Date
    May 2012
    Location
    California, Los Angles (hometown)
    Posts
    1

    Cool Problem solved!

    Hi!
    If you need the answer (after 4 years!) here it is:

    first of all It's not your fault. It's because of inner scripting.
    One second calculates at 995 m.s. So Yeah, u guessed right, It's faster!

    I wrote anathor script (based on this script) and you can find it here:
    http://drsdavidsoft.persiangig.com/P...wn-by-DRS.html

    You can press F5 (even after 10 hours!) and the time won't be changed.

    Hope I helped you.

    Sorry for my bad language! (Because, u know, English is not my native language)
    David Refoua

  3. #3
    Join Date
    May 2012
    Posts
    5
    Hi
    Excellent post

    nice sharing information

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles