Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Live Digital Clock with JavaScript

Status
Not open for further replies.

clarkin

Programmer
Dec 4, 2002
707
IE
From my answer to someone's question in the vbscript forum (thread329-674347).

Try this in a new .html file, it simulates a digital clock using client machine's time:
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd&quot;>[/URL]
<html>
<head>
<title>testbed</title>
<style type=&quot;text/css&quot;>
#divClock {
	font-size: 16px;
	font-family: &quot;Courier New&quot;, monospace;
	color: #333333;
	background-color: #CCFFCC;
	border: 1px solid black;
	width: 55px; /*CSS experts: have this auto somehow? */
}
</style>
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
// global vars to point to clock spans
var hoursObj, sepObj, minsObj;
var rightNow, hours, mins, tmp;

function start() {
	//init references to clock spans
	hoursObj = document.getElementById(&quot;hoursID&quot;);
	sepObj = document.getElementById(&quot;sepID&quot;);
	minsObj = document.getElementById(&quot;minsID&quot;);
	
	//start first time refresh
	setTimeout(&quot;RefreshDateTime()&quot;, 1)
}
function RefreshDateTime() {
	//get current time and split into hours / mins
	rightNow = new Date();
	hours = rightNow.getHours();
	mins = rightNow.getMinutes()
	
	//change to strings with leading zeros if needed
	tmp = &quot;00&quot; + hours;
	hours = tmp.substring(tmp.length-2,tmp.length);
	tmp = &quot;00&quot; + mins;
	mins = tmp.substring(tmp.length-2,tmp.length);
	
	//update hours / mins spans
	hoursObj.firstChild.nodeValue = hours;
	minsObj.firstChild.nodeValue = mins;
	
	//toggle separator every second so it blinks
	toggleSeparator();
	
	//now call this function again in one second
	setTimeout(&quot;RefreshDateTime()&quot;, 1000)
}

function toggleSeparator() {
	//check current status
	tmp = sepObj.firstChild.nodeValue;
	
	//swap to opposite
	if (tmp == &quot;:&quot;)
		tmp = &quot; &quot;;
	else
		tmp = &quot;:&quot;;
		
	//update to new status
	sepObj.firstChild.nodeValue = tmp;
}
</script>
</head>
<body onload=&quot;start();&quot;>
  <div id=&quot;divClock&quot;><span id=&quot;hoursID&quot;>00</span><span id=&quot;sepID&quot;>:</span><span id=&quot;minsID&quot;>00</span></div>
  <p>V2.0: Digital Clock :)<br>
  This example now works using accepted standards and would probably work on a PDA or mobile phone :).</p>
</body>
</html>

improvements/suggestions welcomed.

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top