blackcell Posted February 21, 2008 Share Posted February 21, 2008 <script type="text/javascript"> function startTime() { var today=new Date(); var h=today.getHours(); var m=today.getMinutes(); var s=today.getSeconds(); // add a zero in front of numbers<10 m=checkTime(m); s=checkTime(s); document.getElementById('txt').innerHTML=h+":"+m+":"+s; t=setTimeout('startTime()',500); } function checkTime(i) { if (i<10) { i="0" + i; } return i; } </script> </head> <body onload="startTime()"> <div id="txt"></div> source: http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_clock First, I am very new to javascript. I want to get to know js as well as php (where I don't have to grab examples from free script sites) I have a few questions on how JS works. First, how does the clock update automatically with every second? Second, how does the <div> tag work? Say if I wanted to insert the clock in a table, would I have to have the: <div id="txt"></div> like this: ... <td><div id="txt"></div></td> ... Quote Link to comment Share on other sites More sharing options...
phpQv3.0 Posted February 21, 2008 Share Posted February 21, 2008 The script gets the initial day and time from your computer. Then it uses setTimeout to create a timing event that update the text in your div every 1/2 a second. The DIV tag does not matter; as long as whatever element you want to display the time in; has an id of txt and the element is capable of displaying content in it; then you could use any element. you could just do something like this: <script type="text/javascript"> function startTime() { var today=new Date(); var h=today.getHours(); var m=today.getMinutes(); var s=today.getSeconds(); // add a zero in front of numbers<10 m=checkTime(m); s=checkTime(s); document.getElementById('txt').innerHTML=h+":"+m+":"+s; t=setTimeout('startTime()',500); } function checkTime(i) { if (i<10) { i="0" + i; } return i; } </script> </head> <body onload="startTime()"> <table> <tr> <td id="txt"></td> </tr> </table> or if you don't want to take up any space at all in your page; I suggest using a span, like so: <span id="txt"></span> Quote Link to comment Share on other sites More sharing options...
blackcell Posted February 22, 2008 Author Share Posted February 22, 2008 I see... Now, is the setTimeout some sort of function that executes a piece of script after the specified amount of Time? If this is true it makes sense how it continually updates because the function loops through itself ever 1/2 second using the settimeout right? Quote Link to comment Share on other sites More sharing options...
phpQuestionerThe4th Posted February 22, 2008 Share Posted February 22, 2008 I see... Now, is the setTimeout some sort of function that executes a piece of script after the specified amount of Time? If this is true it makes sense how it continually updates because the function loops through itself ever 1/2 second using the settimeout right? Exactly Right Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.