| | |
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:
Solved Threads: 0
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
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>•
•
Join Date: Oct 2008
Posts: 63
Reputation:
Solved Threads: 0
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script language="JavaScript" type="text/javascript"> function curTime() { var now=new Date() var hrs=now.getHours() var min=now.getMinutes() var sec=now.getSeconds() var don="AM" if (hrs>=12){ don="PM" } if (hrs<10) { hrs="0"+hrs } if (min<10) { min="0"+min } if (sec<10) { sec="0"+sec } clock.innerHTML=hrs+":"+min+":"+sec+" "+don setTimeout("curTime()",1000) } </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
Hope this little code will help you:
html Syntax (Toggle Plain Text)
<html> <head> <style> div#clock { position: absolute; border: 2px solid ccc; background-color: c0f; text-color: 000; width: auto; right: 2%; } </style> <script language="javascript"> var clockDiv; function initiateClock() { clockDiv = document.getElementById('clock'); if(!clockDiv) return; updateClock(); setInterval(function(){updateClock();}, 1000); } function updateClock() { var time = new Date(); var sep = ":"; var timeStr = (time.getHours() < 10 ? "0" + time.getHours() : (time.getHours() > 12) ? time.getHours() - 12 : time.getHours()) + sep + (time.getMinutes() < 10 ? "0" + time.getMinutes() : time.getMinutes()) + sep + (time.getSeconds() < 10 ? "0" + time.getSeconds() : time.getSeconds()); clockDiv.innerHTML = timeStr; } </script> </head> <body onload='initiateClock();'> <div id='clock'></div> </body> </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.
•
•
Join Date: Aug 2009
Posts: 12
Reputation:
Solved Threads: 1
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
http://sites.google.com/site/superco...vascript_clock
cheers,
fuego
![]() |
Similar Threads
- Can I use a template html page within another html page (Site Layout and Usability)
- do I need to put keyword tag on every single page? (Search Engine Optimization)
- to display the running time as clock in page (PHP)
- Can we put 2 buttonlink in only 1 page (ASP.NET)
- Align floated images in center of page with CSS (HTML and CSS)
- Flash/Javascript/HTML code for gallery page (Web Development Job Offers)
- PHP in a html page? (PHP)
- Grab info from vB and put into HTML page (Growing an Online Community)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: how to change id by adding new row..
- Next Thread: Exception handling statement.
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically browser bug calendar captchaformproblem checkbox child class close cookies createrange() cursor date debugger dependent disablefirebug dom dropdown editor element embed engine events explorer ext file form forms getselection google gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe image() images internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jump libcurl maps math media microsoft object onerror onmouseoutdivproblem onreadystatechange parent paypal pdf php player position post programming progressbar rated regex runtime safari scriptlets scroll search security session shopping size software sql star stars synchronous text textarea unicode validation web webservice website windowsxp wysiwyg \n





