943,558 Members | Top Members by Rank

Ad:
Aug 10th, 2009
0

I want put a clock on my page

Expand Post »
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>
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
polo_coins is offline Offline
63 posts
since Oct 2008
Aug 10th, 2009
0

Re: I want put a clock on my page

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
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
polo_coins is offline Offline
63 posts
since Oct 2008
Aug 10th, 2009
0

Re: I want put a clock on my page

Hope this little code will help you:

html Syntax (Toggle Plain Text)
  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.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Aug 19th, 2009
0

Re: I want put a clock on my page

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
fuego2008 is offline Offline
13 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: how to change id by adding new row..
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Exception handling statement.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC