Share on Google+Share on Google+

JavaScript Clock Timer

This page discusses - JavaScript Clock Timer

JavaScript Clock Timer


In this section, we are going to show you a clock timer using JavaScript.

In the given example, we have created the instance of Date to show the time. Here we have created a condition that if the number of minutes, seconds or hours is less than 9, then it will add 0 in front of min, sec or hour value like 09 in the timer and it will add AM or PM according to the hour value. By using the JavaScript method setTimeout("slideImages()",1000), the function displayTime() is called after every one second and performs the desired action when the target time is completed.

Here is the code

<h2>Clock Timer</h2>
<script language="JavaScript" type="text/javascript">
function displayTime() {
var date=new Date();
if (min<=9) {
if (sec<=9) {
if (hour>12) { 
hour=hour-12; add="pm";
else {
hour=hour; add="am"; 
if (hour==12){
time = ((hour<=9) ? "0"+hour : hour) + ":" + min + ":" + sec + " " + add;
if (document.getElementById){ 
document.getElementById('timer').innerHTML = time; 
else if (document.layers) {
setTimeout("displayTime()", 1000);
window.onload = displayTime;
<span id="timer" style="position:absolute; left:10; font-family: ariel roman; font-size: 18pt; color:white; background:black"></span>

Output will be displayed as:

Download Source Code:



Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: JavaScript Clock Timer  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image