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 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript help ?

    I made a clock using JavaScript, the code is this :

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta name="generator" content="Adobe GoLive" />
    <title>Digital Clock</title>
    
    
    <script language="JavaScript">
    
    
    
    
    var time = new Date()
    var hourx = time.getHours()
    var minutes = time.getMinutes()
    
    
    function minutes_2()
    {
    switch (minutesx){
    case 0:
    document.write("<img rc='0.gif'>" ,"<img src='0.gif'>");
    break
    case 1:
    document.write("<img src='0.gif'>" ,"<img src='1.gif'>");
    break
    case 2:
    document.write("<img src='0.gif'>" ,"<img src='2.gif'>");
    break
    case 3:
    document.write("<img src='0.gif'>" ,"<img src='3.gif'>");
    break
    case 4:
    document.write("<img src='0.gif'>", "<img src='4.gif'>");
    break
    case 5:
    document.write("<img src='0.gif'>", "<img src='5.gif'>");
    break
    case 6:
    document.write("<img src='0.gif'>", "<img src='6.gif'>");
    break
    case 7:
    document.write("<img src='0.gif'>", "<img src='7.gif'>");
    break
    case 8:
    document.write("<img src='0.gif'>", "<img src='8.gif'>");
    break
    case 9:
    document.write("<img src='0.gif'>" ,"<img src='9.gif'>");
    break
    case 10:
    document.write("<img src='1.gif'>" ,"<img src='0.gif'>");
    break
    case 11:
    document.writeln("<img src='1.gif'>", "<img src='1.gif'>");
    break
    case 12:
    document.write("<img src='1.gif'>", "<img src='2.gif'>");
    break
    case 13:
    document.writeln("<img src='1.gif'>", "<img src='3.gif'>");
    break
    case 14:
    document.write("<img src='1.gif'>", "<img src='4.gif'>");
    break
    case 15:
    document.write("<img src='1.gif'>", "<img src='5.gif'>");
    break
    case 16:
    document.write("<img src='1.gif'>", "<img src='6.gif'>");
    break
    case 17:
    document.write("<img src='1.gif'>", "<img src='7.gif'>");
    break
    case 18:
    document.write("<img src='1.gif'>", "<img src='8.gif'>");
    break
    case 19:
    document.write("<img src='1.gif'>", "<img src='9.gif'>");
    break
    case 20:
    document.write("<img src='2.gif'>", "<img src='0.gif'>");
    break
    case 21:
    document.write("<img src='2.gif'>", "<img src='1.gif'>");
    break
    case 22:
    document.write("<img src='2.gif'>", "<img src='2.gif'>");
    break
    case 23:
    document.write("<img src='2.gif'>", "<img src='3.gif'>");
    break
    case 24:
    document.write("<img src='2.gif'>", "<img src='5.gif'>");
    break
    case 25:
    document.write("<img src='2.gif'>", "<img src='5.gif'>");
    break
    case 26:
    document.write("<img src='2.gif'>", "<img src='6.gif'>");
    break
    case 27:
    document.write("<img src='2.gif'>", "<img src='7.gif'>");
    break
    case 28:
    document.write("<img src='2.gif'>", "<img src='8.gif'>");
    break
    case 29:
    document.write("<img src='2.gif'>", "<img src='9.gif'>");
    break
    case 30:
    document.write("<img src='3.gif'>", "<img src='0.gif'>");
    break
    case 31:
    document.write("<img src='3.gif'>", "<img src='1.gif'>");
    break
    case 32:
    document.write("<img src='3.gif'>", "<img src='2.gif'>");
    break
    case 33:
    document.write("<img src='3.gif'>", "<img src='3.gif'>");
    break
    case 34:
    document.write("<img src='3.gif'>", "<img src='4.gif'>");
    break
    case 35:
    document.write("<img src='3.gif'>", "<img src='5.gif'>");
    break
    case 36:
    document.write("<img src='3.gif'>", "<img src='6.gif'>");
    break
    case 37:
    document.write("<img src='3.gif'>", "<img src='7.gif'>");
    break
    case 38:
    document.write("<img src='3.gif'>", "<img src='8.gif'>");
    break
    case 39:
    document.write("<img src='3.gif'>", "<img src='9.gif'>");
    break
    case 40:
    document.write("<img src='4.gif'>", "<img src='0.gif'>");
    break
    case 41:
    document.write("<img src='4.gif'>", "<img src='1.gif'>");
    break
    case 42:
    document.write("<img src='4.gif'>", "<img src='2.gif'>");
    break
    case 43:
    document.write("<img src='4.gif'>", "<img src='3.gif'>");
    break
    case 44:
    document.write("<img src='4.gif'>", "<img src='4.gif'>");
    break
    case 45:
    document.write("<img src='4.gif'>", "<img src='5.gif'>");
    break
    case 46:
    document.write("<img src='4.gif'>", "<img src='6.gif'>");
    break
    case 47:
    document.write("<img src='4.gif'>", "<img src='7.gif'>");
    break
    case 48:
    document.write("<img src='4.gif'>", "<img src='8.gif'>");
    break
    case 49:
    document.write("<img src='4.gif'>", "<img src='9.gif'>");
    break
    case 50:
    document.write("<img src='5.gif'>", "<img src='0.gif'>");
    break
    case 51:
    document.write("<img src='5.gif'>", "<img src='1.gif'>");
    break
    case 52:
    document.write("<img src='5.gif'>", "<img src='2.gif'>");
    break
    case 53:
    document.write("<img src='5.gif'>", "<img src='3.gif'>");
    break
    case 54:
    document.write("<img src='5.gif'>", "<img src='4.gif'>");
    break
    case 55:
    document.write("<img src='5.gif'>", "<img src='5.gif'>");
    break
    case 56:
    document.write("<img src='5.gif'>", "<img src='6.gif'>");
    break
    case 57:
    document.write("<img src='5.gif'>", "<img src='7.gif'>");
    break
    case 58:
    document.write("<img src='5.gif'>", "<img src='8.gif'>");
    break
    case 59:
    document.write("<img src='5.gif'>", "<img src='9.gif'>");
    break
    default: document.write("Clock contains errors")
    }
    
    }
    
    function displayClock()
    {
    switch (hourx) {
    
    case 0:
    document.write("<img src='1.gif'>", "<img src='2.gif'>");
    break
    
    case 1:
    document.write("<img src='0.gif'>", "<img src='1.gif'>");
    break
    
    case 2:
    document.write("<img src='0.gif'>", "<img src='2.gif'>");
    
    break
    case 3:
    document.write("<img src='0.gif'>", "<img src='3.gif'>");
    
    break
    case 4:
    document.write("<img src='0.gif'>", "<img src='4.gif'>");
    
    break
    case 5:
    document.write("<img src='0.gif'>", "<img src='5.gif'>");
    
    break
    case 6:
    document.write("<img src='0.gif'>", "<img src='6.gif'>");
    
    break
    case 7:
    document.write("<img src='0.gif'>", "<img src='7.gif'>");
    
    break
    case 8:
    document.write("<img src='0.gif'>", "<img src='8.gif'>");
    
    break
    case 9:
    document.write("<img src='0.gif'>", "<img src='9.gif'>");
    
    break
    case 10:
    document.write("<img src='1.gif'>", "<img src='0.gif'>");
    
    break
    case 11:
    document.write("<img src='1.gif'>", "<img src='1.gif'>");
    
    break
    case 12:
    document.write("<img src='1.gif'>", "<img src='2.gif'>");
    
    break
    case 13:
    document.write("<img src='0.gif'>", "<img src='1.gif'>");
    
    break
    case 14:
    document.write("<img src='0.gif'>", "<img src='2.gif'>");
    
    break
    case 15:
    document.write("<img src='0.gif'>", "<img src='3.gif'>");
    
    break
    case 16:
    document.write("<img src='0.gif'>", "<img src='4.gif'>");
    
    break
    case 17:
    document.write("<img src='0.gif'>", "<img src='5.gif'>");
    
    break
    case 18:
    document.write("<img src='0.gif'>", "<img src='6.gif'>");
    
    break
    case 19:
    document.write("<img src='0.gif'>", "<img src='7.gif'>");
    
    break
    case 20:
    document.write("<img src='0.gif'>", "<img src='8.gif'>");
    
    break
    case 21:
    document.write("<img src='0.gif'>", "<img src='9.gif'>");
    
    break
    case 22:
    document.write("<img src='1.gif'>", "<img src='0.gif'>");
    
    break
    case 23:
    document.write("<img src='1.gif'>", "<img src='1.gif'>");
    
    break
    default: document.write("Clock is dead");
    }
    }
    
    
    
    
    
    
    
    
    </script>
    </head>
    
    <body>
    
    <script language="JavaScript">
    
    displayClock()+minutes_2()
    
    </script>
    
    </body>
    
    </html>
    I want the time to change automatically as time changes. how do i do that ? the time is displayed using gif. pictures. i tried using setTimeout and setInterval...its still not working...pls help...
    Last edited by joshuadavid2007; 06-18-2007 at 12:25 PM.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function preloadImgs(){
      var arr=new Array();
      for (var i=0; i<=9; i++){
        arr[i] = new Image();
        arr[i].src = i + ".gif";
      }
    }
    
    function displayClock(){
      var d = new Date();
      var h = d.getHours();
      var m = d.getMinutes();
      if (h>12) h-=12;
      h = (h<10) ? "0" + h : "" + h;
      m = (m<10) ? "0" + m : "" + m;
      var arrH = h.split("");
      var arrM = m.split("");
      document.getElementById("clock").innerHTML = '<img src="'+arrH[0]+'.gif" alt="'+arrH[0]+'" /><img src="'+arrH[1]+'.gif" alt="'+arrH[1]+'" /> : <img src="'+arrM[0]+'.gif" alt="'+arrM[0]+'" /><img src="'+arrM[1]+'.gif" alt="'+arrM[1]+'"/>';
    }
    
    preloadImgs();
    
    window.onload = function(){
      setInterval("displayClock()", 1000);
    }
    </script>
    </head>
    <body>
    <div id="clock"></div>
    </body>
    </html>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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