Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Why doesn't this textbook script stop/start clock?

    Hi,
    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(){
    clearInterval(timerID);
    }

    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>
    <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.
    Casseandra

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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.

  • #3
    New Coder
    Join Date
    Apr 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you, that was the problem.

    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.
    Casseandra

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts

    Get a good book

    Never had a JS book but people here recommend JavaScript: The Definitive Guide By David Flanagan. Or you may want to get some suggestions from here.

  • #5
    New Coder
    Join Date
    Apr 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks again

    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.

    Thanks.
    Casseandra

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    You might want to consider liorean's suggestion (2nd link in my previous post) for beginners like you.


  •  

    Posting Permissions

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