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

<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>
3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by fuego2008
0
<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

0

Hope this little code will help you:

<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>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.