Results 1 to 7 of 7

Thread: JavaScript clock code with an unknown problem

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    JavaScript clock code with an unknown problem

    Here's my complete javascript code for a countdown clock. I checked through it over and over and over and I don't see a single thing wrong with it. There's even no evil little semicolons missing. However, when I call the function from in the body IE gives me a terribly descriptive "error on line 1 character 1. Object Expected" error. Why didn't they just replace that one with "I dunno what happened, something messed up." Anyway here's my complete script:

    <script type="text/javascript">
    function CalculateTime()
    {
    var enddate= new Date("May 18, 2007 15:30:00");
    var nowdate = new Date();
    var totalseconds
    //these are the final values
    var Fyears, Fdays, Fhours, Fminutes, Fseconds
    //these are the temporary holders
    var ty, td, th, tm, ts
    //these are the difference holders that hold the chopped off decimals
    var dy, dd, dh, dm, ds
    totalseconds = (enddate.getMilliseconds() - nowdate.getMilliseconds());
    totalseconds = totalseconds / 1000;

    ty = totalseconds / 31536000;
    Fyears = Math.floor(ty);
    dy = ty - Fyears;
    //example: if it divides out to 1.2185 years then years is 1 and dy is 0.2185
    //so now we multiply 0.2185 by the amount of days in a year and get 79.7525 days
    //so then we set days equal to 79 and save the difference of 0.7525 in dd for the next part
    td = dy * 365;
    Fdays = Math.floor(td);
    dd = td - Fdays;

    th = dd * 24;
    Fhours = Math.floor(th);
    dh = th - Fhours;

    tm = dh * 60;
    Fminutes = Math.floor(tm);
    dm = tm - Fminutes;

    ts = dm * 60;
    Fseconds = Math.floor(ts);
    ds = ts - Fseconds;
    //ds should equal 0

    years.innerHTML = Fyears;
    days.innerHTML = Fdays;
    hours.innerHTML = Fhours;
    minutes.innerHTML = Fminutes;
    seconds.innerHTML = Fseconds;
    }

    </script>

    And here's the body in case U want to test it yourself:


    <body onload="setInterval('CalculateTime()', 1000);">
    <h4 align="center">Time Remaining This Semester </h4>
    <table width="400" border="0" align="center" cellpadding="2" cellspacing="2" bgcolor="#CCCC00">
    <tr>
    <th scope="col">Years</th>
    <th scope="col">Days</th>
    <th scope="col">Hours</th>
    <th scope="col">Minutes</th>
    <th scope="col">Seconds</th>
    </tr>
    <tr>
    <td id="years">&nbsp;</td>
    <td id="days">&nbsp;</td>
    <td id="hours">&nbsp;</td>
    <td id="minutes">&nbsp;</td>
    <td id="seconds">&nbsp;</td>
    </tr>
    </table>
    </body>

    P.S. I'll fix it for leapyears later
    Last edited by Desolator144; Oct 17th, 2006 at 01:57 PM.
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

Posting Permissions

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



Click Here to Expand Forum to Full Width