View Full Version : Why doesn't this textbook script stop/start clock?

04-29-2004, 05:51 AM
This script was taken right from the web site of the author of our college textbook we are using in class (The Web Wizard's Guide to JavaScript). I didn't type it, its an example problem to show us how clocks work. The problem is, how are we supposed to learn how to make one ourselves, when this one doesn't work right.

It is a basic clock, two buttons, one starts it and one stops it. Well in theory, it doesn't work,(The clock part does work, just doesn't stop it, its always running) and since I don't know how they are supposed to work, hard for me to know how to correct the problem. The book is showing me the exact same way as the web site code too, so thats no good.

My question is, what did the author miss. This is not my homework. I have to do something totally different, i just would like to know what is wrong with it. I am assuming it is in the onclick event handler or the function itself. This is the functions he used (functions are in the head, onclick in the body).

var timerID = "";
function startClock(){
timerID = setInterval('showTime()',1000);
function stopClock(){

This is the onload and the onclick events.

<body bgcolor="white" onload="startClock();">
<div align="CENTER">
<h1>A Better Clock</h1>

<form name="clock"><input name="face" size="15"><br>
<input type="button" name="startclock" value="Start Clock" onclick="startClock();">
<input type="button" name="stopclock" value="Stop Clock" onclick="stopClock();">

I thought maybe it was because the name"stopclock" name"startclock" were all lowercase and the functions weren't, so I changed those but it still didn't work. Any ideas.

04-29-2004, 06:26 AM
Try removing the onload handler.
What is probably happening is if you click the Start clock button, there are two setIntervals running, causing clearInterval not to work.

04-29-2004, 07:54 AM
Thank you, that was what the problem was, 3 out of 5 of the scripts in this section of the book, don't work. Can't believe they using that to teach us JavaScript, sometimes these languages get confusing enough without the examples being wrong in the first place.

Thanks again.

04-29-2004, 10:30 AM
Never had a JS book but people here recommend JavaScript: The Definitive Guide By David Flanagan (http://www.oreilly.com/catalog/jscript4/). Or you may want to get some suggestions from here (http://www.codingforums.com/showthread.php?t=26551#postmenu_136487).

04-29-2004, 10:18 PM
I'm going to head to the closest "Border's Book Store" and pick up a copy. Then I'm going to take it to the college and tell them to consider using it instead of the book they are using.


04-30-2004, 04:07 AM
You might want to consider liorean's suggestion (2nd link in my previous post) for beginners like you.