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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2014
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Stuck on basic JavaScript

    I am making a basic clock through javascript using setInterval. I can get the time to show up but it will not update, which I am sure means there is something wrong
    with setInterval. I am new to this, so any help to get me in the right direction would be appreciated. Here is what I have so far:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Time</title>



    <script type="text/javascript">
    /* <![CDATA[ */
    {

    var currentTime = new Date();
    var currentHours = currentTime.getHours();
    var currentMinutes = currentTime.getMinutes();
    var currentSeconds = currentTime.getSeconds();
    }



    function newTime(){
    if(currentMinutes < 10)
    currentMinutes = "0" + currentMinutes;
    if(currentSeconds < 10)
    currentSeconds = "0" + currentSeconds;
    if(currentHours > 12)
    currentHours = currentHours - 12;
    var curTime = currentHours + ":" + currentMinutes + ":" + currentSeconds;
    document.write(curTime);
    }
    /* ]]> */
    </script>
    </head>
    <body onload="currentTime; setInterval('newTime()',1000);">

    </body>
    </html>

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    You can't use document.write andsetInterval in the same web page.

    The setInterval call should read:

    setInterval(Time,1000)

    and the function definition should have the word new removed from it. new is used for creating objects not defining functions.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,038
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Quote Originally Posted by felgall View Post

    The setInterval call should read:

    setInterval(Time,1000)

    and the function definition should have the word new removed from it. new is used for creating objects not defining functions.
    newTime is the name of the function not Time. So the setInterval should be:
    Code:
    setInterval(newTime,1000)

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    Quote Originally Posted by glenngv View Post
    newTime is the name of the function not Time. So the setInterval should be:
    Code:
    setInterval(newTime,1000)
    When I read the code it looked like there was a space between new and Time - which demonstrates that was a poor choice of function name as it was extremely easy to misread it.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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