RSS

Tag Archives: Javascript

Add Timer to your page using external javascript

// Put this to an external .js

// Example is save this as script.js then put it on the same path with the html your going to embed this.

window.onload = showTheTime;

function showTheTime() {
var now = new Date();

var theTime = showTheHours(now.getHours()) + showZeroFilled(now.getMinutes()) + showZeroFilled(now.getSeconds()) + showAmPm();
document.getElementById(“showTime”).innerHTML = theTime;
setTimeout(“showTheTime()”,1000);    /* setTimeout gets 2 PARAMETERS. Do the showTheTime FUNCTION every 1000 millisecond or 1 second */

function showTheHours(theHour) {
if (theHour == 0) {
return 12;
}
if (theHour < 13) {
return theHour;
}
return theHour-12;
}

function showZeroFilled(inValue) {
if (inValue > 9) {
return “:” + inValue;
}
return “:0″ + inValue;
}

function showAmPm() {
if (now.getHours() < 12) {
return ” am”;
}
return ” pm”;
}
}

// In HTML

<html>

<head>

<title></title>

<script type=”text/javascript” src=”script.js”></script>

</head>

<body>

<span id=”showTime”></span>

</body>

</html>

 
Leave a comment

Posted by on August 29, 2009 in Javascript

 

Tags: , ,

 
Follow

Get every new post delivered to your Inbox.