I want put a clock on my page

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Oct 2008
Posts: 63
Reputation: polo_coins is an unknown quantity at this point 
Solved Threads: 0
polo_coins polo_coins is offline Offline
Junior Poster in Training

I want put a clock on my page

 
0
  #1
Aug 10th, 2009
I wrote code below it open me a new window and show current time but I want it will show it in same window and chnge each second

here is my code (i think I must use setInterval function to change time each second but if I put the call in body it's still have problem

<img  onmouseover="myFunction()" />
</table>






<script type="text/javascript">
function myFunction()
{
var n=new Date();
//alert("document.write(now.getHours() + "."+now.getMinutes()+"."+now.getSeconds())");

document.write(n.getHours()+":"+n.getMinutes()+":"+n.getSeconds());

}
</script>
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 63
Reputation: polo_coins is an unknown quantity at this point 
Solved Threads: 0
polo_coins polo_coins is offline Offline
Junior Poster in Training

Re: I want put a clock on my page

 
0
  #2
Aug 10th, 2009
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="JavaScript" type="text/javascript">
  2.  
  3. function curTime()
  4. {
  5. var now=new Date()
  6. var hrs=now.getHours()
  7. var min=now.getMinutes()
  8. var sec=now.getSeconds()
  9. var don="AM"
  10. if (hrs>=12){ don="PM" }
  11. if (hrs<10) { hrs="0"+hrs }
  12. if (min<10) { min="0"+min }
  13. if (sec<10) { sec="0"+sec }
  14. clock.innerHTML=hrs+":"+min+":"+sec+" "+don
  15. setTimeout("curTime()",1000)
  16. }
  17.  
  18. </script>


this is a code for perfect watch ,you must put a call in <body> tag
and use <span> tag to locate it on a page
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: I want put a clock on my page

 
0
  #3
Aug 10th, 2009
Hope this little code will help you:

  1. <html>
  2. <head>
  3. <style>
  4. div#clock {
  5. position: absolute;
  6. border: 2px solid ccc;
  7. background-color: c0f;
  8. text-color: 000;
  9. width: auto;
  10. right: 2%;
  11. }
  12. </style>
  13.  
  14. <script language="javascript">
  15. var clockDiv;
  16.  
  17. function initiateClock() {
  18. clockDiv = document.getElementById('clock');
  19. if(!clockDiv) return;
  20.  
  21. updateClock();
  22. setInterval(function(){updateClock();}, 1000);
  23. }
  24.  
  25. function updateClock() {
  26. var time = new Date();
  27. var sep = ":";
  28. var timeStr = (time.getHours() < 10 ? "0" + time.getHours() : (time.getHours() > 12) ? time.getHours() - 12 : time.getHours()) + sep +
  29. (time.getMinutes() < 10 ? "0" + time.getMinutes() : time.getMinutes()) + sep +
  30. (time.getSeconds() < 10 ? "0" + time.getSeconds() : time.getSeconds());
  31.  
  32. clockDiv.innerHTML = timeStr;
  33. }
  34. </script>
  35. </head>
  36. <body onload='initiateClock();'>
  37. <div id='clock'></div>
  38. </body>
  39. </html>
Last edited by Luckychap; Aug 10th, 2009 at 3:30 pm.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 12
Reputation: fuego2008 is an unknown quantity at this point 
Solved Threads: 1
fuego2008 fuego2008 is offline Offline
Newbie Poster

Re: I want put a clock on my page

 
0
  #4
Aug 19th, 2009
There is a nice tutorial about JS clock in this link, I hope it would have good use for you:

http://sites.google.com/site/superco...vascript_clock

cheers,
fuego
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC