|
-
Oct 17th, 2006, 01:46 PM
#1
Thread Starter
Hyperactive Member
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"> </td>
<td id="days"> </td>
<td id="hours"> </td>
<td id="minutes"> </td>
<td id="seconds"> </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 
-
Oct 18th, 2006, 10:45 PM
#2
Thread Starter
Hyperactive Member
-
Oct 19th, 2006, 12:17 AM
#3
Re: JavaScript clock code with an unknown problem
You don't reference elements like you did there. Try:
document.getElementById('years').innerHTML = fyears;
and so on.
-
Oct 19th, 2006, 12:56 AM
#4
New Member
Re: JavaScript clock code with an unknown problem
Don't use InnerHTML its bad. You have missing in semi colons on several lines.
-
Oct 19th, 2006, 01:57 AM
#5
Re: JavaScript clock code with an unknown problem
There are semi-colons missing. Namely after your var lines. But, they're not required anyway.
Run your script in Firefox with the Firebug extension to debug it properly.
And please post using the [code] tags.
-
Oct 23rd, 2006, 01:05 PM
#6
Thread Starter
Hyperactive Member
Re: JavaScript clock code with an unknown problem
well I changed it to:
Code:
document.getElementById('years').innerHTML = Fyears;
document.getElementById('days').innerHTML = Fdays;
document.getElementById('hours').innerHTML = Fhours;
document.getElementById('minutes').innerHTML = Fminutes;
document.getElementById('seconds').innerHTML = Fseconds;
and it still does the same because the way I had it is a way to do it and has worked every other time in the past. And if I shouldn't use innerHTML then what's another way to stick content inside a cell? Btw innerHTML has worked every time in the past too. I'll go semi-colon the hell out of it but I don't think that will help either As for firebug, it can't find any javascript on the page but there is a new error every second. The debug window is completely blank and the drop down menu for selecting scripts was placed in such a great place that it opens outside the bounds of the window. Seriously, how did that make it past testing? Open source programmers are really something. Anyway, I used the arrow keys and the script selector menu is actually empty. I did tell it to break on error and it kinda sorta hinted at the fact that the problem lies with the frame element inside "this" equalling null but there are no frames on the page so that's a little odd. And it says on the bottom Call Stack (null) default.htm line 1, which is about as useful. I never referenced my page itself anywhere in the code except I suppose with "document.getElementById('years').innerHTML = Fyears;" and yes, something by that name exists so this really isn't terribly helpful. It doesn't even seem to want to tell me what line really actually caused the error.
Update:
OMG I finally figured out what was wrong by absolute random chance. My script tags were inside my style tags grrrrrrr THANKS DREAMWEAVER. That explains why every debugger was like duuuhhhh iunno. Of course, it displays -1 years and doesn't tick so there's apparently a little wrong with the logic
Last edited by Desolator144; Oct 23rd, 2006 at 01:11 PM.
I tried to end process on Visual Studio 2005
but PETA stopped me saying it's smart enough
to be a living creature 
-
Oct 23rd, 2006, 06:50 PM
#7
Re: JavaScript clock code with an unknown problem
And if I shouldn't use innerHTML then what's another way to stick content inside a cell?
DOM data property of text nodes
Code:
var years = document.getElementById('years');
years.firstChild.data = Fyears; // if the text node is the first thing within it
Use the DOM Inspector in Firefox to see the text nodes.
As for firebug, it can't find any javascript on the page but there is a new error every second. The debug window is completely blank and the drop down menu for selecting scripts was placed in such a great place that it opens outside the bounds of the window. Seriously, how did that make it past testing? Open source programmers are really something.
It doesn't open outside the bounds, it opens with no content since your script tags were misplaced. I agree it looks misleading, but its not really designed to work when there are no scripts present.
I'm also an open source programmer, so watch what you say if you want assistance.
OMG I finally figured out what was wrong by absolute random chance. My script tags were inside my style tags grrrrrrr THANKS DREAMWEAVER. That explains why every debugger was like duuuhhhh iunno.
Duuuhhhh, don't use code generators.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|