SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Printing the time

    I am trying to print the time on my site but it isn't working.
    PHP Code:
    <script language="JavaScript">
      <--
        function 
    time() {
            var 
    time = new Date()
            var 
    theHour time.getHours()
            var 
    theMinute time.getMinutes()
            var 
    currentTime "" + ((theHour 12) ? theHour 12 theHour}
                
    currentTime += ((theMinute 10) ? ":0" ":") + theMinute
                currentTime 
    += (theHour >= 12) ? " pm" " am"
            
    window.writein(currentTime)
            
    // Keeps timer going
            
    timerID setTimeout("time()"1000)
        }
      -->
    </script> 
    Any ideas on how I can fix this?
    Last edited by notepad_coder; Apr 10, 2002 at 04:27.
    - the lid is off the maple syrup again!

  2. #2
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code worked for me after I made 3 changes:

    1. Make sure that you 'comment' your function for older browsers like this:

    Code:
      <script language="JavaScript">
      <!--
        function time() {
            ...
        }
      -->
    </script>
    Not like this:

    Code:
      <script language="JavaScript">
      <--
        function time() {
            ...
        }
      -->
    </script>

    2. Instead of using this line:

    window.writein(currentTime);

    I used this:

    divMyNewDivTag.innerHTML = currentTime;

    Then, within the body tags i added my div tag:
    <div id="divMyNewDivTag"></div>

    3. You have a syntax error on this line:

    var currentTime = "" + ((theHour > 12) ? theHour - 12 : theHour}

    You need to change the curly bracket at the end to a closed parentheses:

    var currentTime = ((theHour > 12) ? theHour - 12 : theHour);



    The time was correct and it updated itself each minute.

    PS Good coding practice to end your lines with a semi-colon ';'. I am neurotic about that...sorry.
    Last edited by makeda; Apr 10, 2002 at 05:02.

  3. #3
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea I usualy end with ; sinse I'm used to coding in PHP but the book I have on JavaScript doesn't do it that way. Thanks for the help, it works now
    Last edited by notepad_coder; Apr 10, 2002 at 15:17.
    - the lid is off the maple syrup again!


Bookmarks

Posting Permissions

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