Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help in basic clock

Status
Not open for further replies.

danielrox

Programmer
Sep 29, 2009
3
CA
i'm trying to make simple clock but i cant get it to work. Can anyone help me out?

<html>
<head>
<script language="JavaScript">

var timerID = null;
var timerRunning = false;

function stopClock() {
if(timerRunning)
clearTimeout (timerID)
timerRunning = false
}

function startClock() {
stopClock()
showtime()
}

function showtime() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var timevalue = "" + ((hours > 12) ? hours - 12 : hours);
timevalue += ((minutes < 10) ? ":0" : ":") + minutes
timevalue += ((seconds < 10) ? ":0" : ":") + seconds
timevalue += (hours >= 12) ? "P.M." : “A.M."
document.Clock.face.value = timeValue
timerID = setTimeout("showtime()",1000)
timerRunning = true
}
</script></head>
<body onLoad="startClock()" bgcolor="beige">
<form name="Clock" onSubmit="0">
<input type="text" name="face" size="11" value="....Initializing...">
</form>
</body>
</html>
 
P.S

All i get is a screen with a beige background in which there is a little text box which says
....Initializing...


 
There are so many things wrong I'd be surprised if it did work.

Code:
var timerID = null;
var timerRunning = false;

function stopClock() {
    if(timerRunning){
        clearTimeout (timerID)[red];[/red]
}
    timerRunning = false[red];[/red]
}
    
function startClock() {
    stopClock()[red];[/red]
    showtime()[red];[/red]
}

function showtime() {
    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();
    var timevalue = "" + ((hours > 12) ? hours - 12 : hours);
    timevalue += ((minutes < 10) ? ":0" : ":") + minutes[red];[/red]
    timevalue += ((seconds < 10) ? ":0" : ":") + seconds[red];[/red]
    [blue]timevalue[/blue] += (hours >= 12) ? "P.M." : "A.M."[red];[/red]
    document.Clock.face.value = [blue]timevalue[/blue][red];[/red]
    timerID = setTimeout("showtime()",1000)[red];[/red]
    timerRunning = true[red];[/red]
}


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
The code I posted, works for me in both IE and FF.
So unless there is something else that you haven't posted it should work.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
School work? How about asking someone else in class, or the teacher/professor for help?

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top