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:
improvements/suggestions welcomed.
Posting code? Wrap it with code tags: [ignore]
[ignore][/code][/ignore].
Try this in a new .html file, it simulates a digital clock using client machine's time:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<title>testbed</title>
<style type="text/css">
#divClock {
font-size: 16px;
font-family: "Courier New", monospace;
color: #333333;
background-color: #CCFFCC;
border: 1px solid black;
width: 55px; /*CSS experts: have this auto somehow? */
}
</style>
<script language="JavaScript" type="text/javascript">
// 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("hoursID");
sepObj = document.getElementById("sepID");
minsObj = document.getElementById("minsID");
//start first time refresh
setTimeout("RefreshDateTime()", 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 = "00" + hours;
hours = tmp.substring(tmp.length-2,tmp.length);
tmp = "00" + 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("RefreshDateTime()", 1000)
}
function toggleSeparator() {
//check current status
tmp = sepObj.firstChild.nodeValue;
//swap to opposite
if (tmp == ":")
tmp = " ";
else
tmp = ":";
//update to new status
sepObj.firstChild.nodeValue = tmp;
}
</script>
</head>
<body onload="start();">
<div id="divClock"><span id="hoursID">00</span><span id="sepID">:</span><span id="minsID">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