Page 1 of 1

Re: Little javascript help

PostPosted: Wed Dec 16, 2009 10:55 pm
by Blink
I don't mean to sound patronising but - http://www.google.co.uk/search?sourceid ... ript+timer

That's where I would start

Re: Little javascript help

PostPosted: Wed Dec 16, 2009 11:09 pm
by Surfa
I don't know java script but could you not use something like a while loop.
Code: Select all
while(IsRunning())
{
//Do timer stuff
}
bool IsRunning()
{
if(time = end time)
return false;
else
retun true;
}

Or something similar.

Re: Little javascript help

PostPosted: Wed Dec 16, 2009 11:12 pm
by Blink
caseyw wrote:Thanks, but I have been looking around, I've found several tutorials, but for some odd reason it always seems like it's about to work then it breaks. I think I might have messed a few other things up, so I'll keep trying.


Maybe you could post what you have so far..?

Re: Little javascript help

PostPosted: Thu Dec 17, 2009 12:14 am
by Blink
Yes, but we can't help if we can't see your code...

Re: Little javascript help

PostPosted: Thu Dec 17, 2009 1:05 am
by Blink
caseyw wrote:My code for the javascript is inside my HTML in the head. It's in one of my earlier posts. Check up some. If you meant something else then please specify please.


Well, looking at the post times you clearly added that code after I made my post so why are bolding that statement as if it was my error? :roll:

Anyway, on to your actual code. I've just had a look and you've got two glaring syntax issues.

Function names shouldn't have a semi colon after them when you're declaring them, and you don't refer to an element id with a hash unless you're using jQuery or something.

Fixing those errors results in it working as expected:

Code: Select all
<html>

<head>

<!-- JavaScript -->
<script type="text/javascript">

function updateTime() {

// Initilization(s)
now = new Date();
timetype = "null";

// Definitions
now_sec = now.getSeconds();
now_min = now.getMinutes();
now_hur = now.getHours();

// Single Digit Correction
if (now_sec<10) {
   now_sec = "0" + now_sec;
}

if (now_min<10) {
   now_min = "0" + now_min;
}

if (now_hur<10) {
   now_hur = "0" + now_hur;
}

// Determine AM & PM
if (now_hur>=12) {
   time_type = "PM";
} else {
   time_type = "AM";
}

// Combine all parts to create Current Time
cur_tim = now_hur + ":" + now_min + ":" + now_sec + " " + time_type;

// Change timer span to current time
document.getElementById("timer").innerHTML = cur_tim;

// Update interval
setTimeout("updateTime()", 0);
}

</script>

<!-- Stylesheets -->
<link rel="stylesheet" href="default.css" media="all" />

</head>

<body onload="updateTime();">

<span id="timer"></span>

</body>

</html>


Are you using Firebug for Firefox? It provides a very useful JS error console.

Re: Little javascript help

PostPosted: Thu Dec 17, 2009 8:31 pm
by zombie@computer
settimeout with 0 could result in mayor performace loss. Please avoid using 0 here.