1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Javascript clock problem

Discussion in 'Software Development' started by Firestone, Feb 11, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. Firestone

    Firestone Thread Starter

    Joined:
    Dec 24, 2006
    Messages:
    32
    I'm trying to make a javascript clock when you click the link. I wrote this code, and always get an error on line 13, "function displayTime() not defined." its defined, so I don't know the problem.
    Code:
    <script type = "text/javascript">
    function displayTime()
    {
    var info = new Date();
    var hours = info.getHours();
    var secs = info.getSeconds();
    var mins = info.getMinutes();
    hours = checkHours(hours);
    secs = checkZero(secs);
    mins = checkZero(mins);
    document.write(hours+":"+mins+":"+secs);
    var t = setTimeout('displayTime()', 500);
    }
    
    function checkHours(h)
    {
    if (h>12)
    {
    h = h - 12;
    }
    return h;
    }
    
    
    function checkZero(x)
    {
    if (x<10)
    {
    x = "0"+x;
    }
    return x;
    }
    </script>
    
    <body>
    <a href = "javascript:displayTime()">Clock</a>
    </body>
    
     
  2. cpscdave

    cpscdave

    Joined:
    Feb 25, 2004
    Messages:
    444
    what browser you trying to run this in? I tried it in IE and it worked with no problem.

    Is the server set up to handle jscript?
     
  3. Firestone

    Firestone Thread Starter

    Joined:
    Dec 24, 2006
    Messages:
    32
    I tried it in FF and IE, and neither worked. I got the error when I was using Firebug on FF.
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Fi Firestone,

    If you have a popup blocker installed on your computer, it's possible it prevents Javascript from running. Also, make sure that Javascript is allowed in your browser(s).
     
  5. Firestone

    Firestone Thread Starter

    Joined:
    Dec 24, 2006
    Messages:
    32
    Javascript is allowed in both of my browsers. The link works, and the clock displays the current time, but doesn't refresh, (setTimeout)...
     
  6. Firestone

    Firestone Thread Starter

    Joined:
    Dec 24, 2006
    Messages:
    32
    I rewrote it using the document.getElementById and it worked, thanks for the help guys.
    Heres the code:

    Code:
    <html>
    <script language = "JavaScript">
    function dispTime()
    {
    var d = new Date();
    var h = d.getHours();
    var m = d.getMinutes();
    var s = d.getSeconds();
    h = checkHours(h);
    m = checkZero(m);
    s = checkZero(s);
    document.getElementById('txt').innerHTML = h+":"+m+":"+s
    t = setTimeout('dispTime()', 500);
    }
    
    function checkHours(x)
    {
    if (x>12)
    {
    x = x - 12;
    }
    return x;
    }
    
    function checkZero(y)
    {
    if (y<10)
    {
    y = "0"+y;
    }
    return y;
    }
    
    </script>
    
    <body onload = "dispTime()">
    <div id = "txt">
    </div>
    </body>
    
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/543281

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice