Countdown Timer problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bonnett

    Countdown Timer problems

    I have been creating a generic countdown timer (source code below), counting
    the seconds, minutes, hours and days till an event, but I have having
    trouble with it finding out how many hours are left. When i get it to
    display a complete countdown (days hours mins seconds left) it is one hour
    short when counting dates that fall in the following time brackets:
    2004,2,29 -> 2004,9,31
    2005,2,28 -> 2005,9,30
    2006,2,27 -> 2006,9,29
    etc..

    I have tested this on mozilla firebird 0.7, and internet explorer 5.5 + 6
    and the problem always occurs. Is this a bug in javascript, or just my bad
    mathematics in finding the modulo value.


    Source code:

    <HTML>
    <HEAD>
    <TITLE>JavaScri pt Countdown</TITLE>
    <SCRIPT LANGUAGE=JAVASC RIPT TYPE="TEXT/JAVASCRIPT">
    <!--
    <!-- Basic Countdown Timer//-->
    <!-- By Peter Bonnett (PeterBonnett@h otmail.com) //-->
    <!-- http://uk.geocities.com/peterbonnett //-->
    var theTime="";
    var type = "theLot"//Default is the lot

    function getType(){
    if (document.theFo rm.changeType[0].checked) {
    type = "seconds"
    }
    else if (document.theFo rm.changeType[1].checked) {
    type = "mins"
    }
    else if (document.theFo rm.changeType[2].checked) {
    type = "hours"
    }
    else if (document.theFo rm.changeType[3].checked) {
    type = "days"
    }
    else if (document.theFo rm.changeType[4].checked) {
    type = "theLot"
    }
    }

    function showTheTime() {
    getType();//See what radio button is checked
    now = new Date
    next = new Date(2004,2,8); //Date you are counting down to
    if(type=="secon ds"){
    document.getEle mentById("count down").innerHTM L =
    parseInt((next. getTime() - now.getTime())/1000)+" seconds"; //Set text to
    show seconds left
    }
    else if(type=="mins" ){
    document.getEle mentById("count down").innerHTM L =
    parseInt((next. getTime() - now.getTime())/(1000*60))+" mins"; //Set text to
    show minutes left
    }
    else if(type=="hours "){
    document.getEle mentById("count down").innerHTM L =
    parseInt((next. getTime() - now.getTime())/(1000*60*60))+" hours"; //Set text
    to show hours left
    }
    else if(type=="days" ){
    document.getEle mentById("count down").innerHTM L =
    parseInt((next. getTime() - now.getTime())/(1000*60*60*24) )+" days"; //Set
    text to show days left
    }
    else if(type=="theLo t"){
    days = parseInt((next. getTime() - now.getTime())/(1000*60*60*24) );
    //Calculation to get the days
    hrsLeft = parseInt((next. getTime() -
    now.getTime())/(1000*60*60))%2 4;//Calculation to get the hours left
    minsLeft = parseInt((next. getTime() -
    now.getTime())/(1000*60))%60;//Calculation to get the minutes left
    secsLeft = parseInt((next. getTime() -
    now.getTime())/(1000))%60;//Calculation to get the seconds left
    document.getEle mentById("count down").innerHTM L = days + " days " + hrsLeft
    + " hours " + minsLeft + " minutes " + secsLeft +" seconds. ";
    }
    setTimeout("sho wTheTime()",100 0)

    }

    //-->
    </SCRIPT>
    </HEAD>
    <BODY BGCOLOR=WHITE onLoad="showThe Time()">
    <DIV ID="countdown"> <!-- Where the time left is displayed//--></DIV>
    <FORM NAME="theForm">
    Choose type:
    <INPUT TYPE=RADIO NAME="changeTyp e">Seconds
    <INPUT TYPE=RADIO NAME="changeTyp e">Minutes
    <INPUT TYPE=RADIO NAME="changeTyp e">Hours
    <INPUT TYPE=RADIO NAME="changeTyp e">Days
    <INPUT TYPE=RADIO NAME="changeTyp e" CHECKED>The Lot
    </FORM>
    </BODY>
    </HTML>



  • Lee

    #2
    Re: Countdown Timer problems

    Bonnett said:[color=blue]
    >
    >I have been creating a generic countdown timer (source code below), counting
    >the seconds, minutes, hours and days till an event, but I have having
    >trouble with it finding out how many hours are left. When i get it to
    >display a complete countdown (days hours mins seconds left) it is one hour
    >short when counting dates that fall in the following time brackets:
    >2004,2,29 -> 2004,9,31
    >2005,2,28 -> 2005,9,30
    >2006,2,27 -> 2006,9,29
    >etc..
    >
    >I have tested this on mozilla firebird 0.7, and internet explorer 5.5 + 6
    >and the problem always occurs. Is this a bug in javascript, or just my bad
    >mathematics in finding the modulo value.[/color]

    Fall back in Fall. Spring forward in Spring.

    Comment

    • Bonnett

      #3
      Re: Countdown Timer problems

      "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
      news:bu4bok01v2 n@drn.newsguy.c om...[color=blue]
      > Bonnett said:[color=green]
      > >
      > >I have been creating a generic countdown timer (source code below),[/color][/color]
      counting[color=blue][color=green]
      > >the seconds, minutes, hours and days till an event, but I have having
      > >trouble with it finding out how many hours are left. When i get it to
      > >display a complete countdown (days hours mins seconds left) it is one[/color][/color]
      hour[color=blue][color=green]
      > >short when counting dates that fall in the following time brackets:
      > >2004,2,29 -> 2004,9,31
      > >2005,2,28 -> 2005,9,30
      > >2006,2,27 -> 2006,9,29
      > >etc..
      > >
      > >I have tested this on mozilla firebird 0.7, and internet explorer 5.5 + 6
      > >and the problem always occurs. Is this a bug in javascript, or just my[/color][/color]
      bad[color=blue][color=green]
      > >mathematics in finding the modulo value.[/color]
      >
      > Fall back in Fall. Spring forward in Spring.
      >[/color]

      *groans*

      Thank you, i shall have to remember to put a little message on the page
      informing anyone who uses it

      Bonnett


      Comment

      • Dr John Stockton

        #4
        Re: Countdown Timer problems

        JRS: In article <bu4d02$dop56$1 @ID-150632.news.uni-berlin.de>, seen in
        news:comp.lang. javascript, Bonnett <PeterBonnettNO SPAM@hotmail.co m>
        posted at Wed, 14 Jan 2004 21:41:05 :-[color=blue]
        >"Lee" <REM0VElbspamtr ap@cox.net> wrote in message
        >news:bu4bok01v 2n@drn.newsguy. com...[color=green]
        >> Bonnett said:[color=darkred]
        >> >
        >> >I have been creating a generic countdown timer (source code below),[/color][/color]
        >counting[color=green][color=darkred]
        >> >the seconds, minutes, hours and days till an event, but I have having
        >> >trouble with it finding out how many hours are left. When i get it to
        >> >display a complete countdown (days hours mins seconds left) it is one[/color][/color]
        >hour[color=green][color=darkred]
        >> >short when counting dates that fall in the following time brackets:
        >> >2004,2,29 -> 2004,9,31
        >> >2005,2,28 -> 2005,9,30
        >> >2006,2,27 -> 2006,9,29
        >> >etc..
        >> >
        >> >I have tested this on mozilla firebird 0.7, and internet explorer 5.5 + 6
        >> >and the problem always occurs. Is this a bug in javascript, or just my[/color][/color]
        >bad[color=green][color=darkred]
        >> >mathematics in finding the modulo value.[/color]
        >>
        >> Fall back in Fall. Spring forward in Spring.[/color]
        >
        > Thank you, i shall have to remember to put a little message on the page
        >informing anyone who uses it[/color]


        Obviously you did not read the newsgroup FAQ carefully enough; nor
        articles posted here within the last few days.

        There is no need for a little message; just code it right.

        You have a choice: assuming you are counting to midwinter, and it is now
        summer, do you or do you not want to show the extra hour?

        To show it, just use the UTC functions throughout.

        To avoid it, do not use getTime, but do long-arithmetic subtraction of
        the fields of the current civil time from those of the target. This
        becomes almost trivial if the target is a midnight - but first, convert
        from Y M D to days.


        One should not use parseInt to make a number an integer; Math.floor is
        provided for that.

        You have next = new Date(2004,2,8) ;
        next = new Date("2004/03/08")
        while longer is less likely to be in error.

        Don't put 1000 as the second parameter of setTimeout; one can easily do
        better - see below.

        Much of the text of showTheTime is repeated, and could be outside the
        conditionals: e.g. use one var T = document.getEle mentById("count down")
        and several T.innerHTML = ... ; use one
        var DT = (next.getTime() - now.getTime()) and several DT.

        ..innerHTML does not work on all browsers; see FAQ discussion in this
        newsgroup.

        Don't let your newsreader wrap long lines; do it yourself, where best
        for legibility. Those who wish to test your code, or modifications to
        it, should not be forced to repair it first.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        Working...