Categories
JavaScript

jsClock – JavaScript Clock with few options

I needed a Clock that is updating automatically and takes the time from the server (same as the office), also they want to display the time in other countries to have a reference.

For this I developed a “simple” (how much I like this word) script to update times.

Screenshot:

Demo
Download

Installation:
Download, unzip and serve your self!

UPDATE: Faster way, just copy paste this in your site:



Usage:

This is the main function that is calling her self all time.

function moveClock(idClock,startTime){//move given clock
	var timeout=1000;//miliseconds to repeat the function

	if ( startTime === undefined ) {//just take the browser time
	    	rightNow = new Date();
		hour = rightNow.getHours();
		minute = rightNow.getMinutes();
		second = rightNow.getSeconds();
	}
	else{//starttime set
		rightNow = startTime.split(':',3);
		hour = parseInt(rightNow[0],10);
		minute =  parseInt(rightNow[1],10);
		second =  parseInt(rightNow[2],10);
		if (second==59){
			if (minute==59){
				if (hour==23) hour=0;
				else hour++;
				minute=0;
			}else minute++;
			second=0;
		}else second++;
	}

	if (minute<10) minute= "0" + minute;
	if (second<10) second= "0" + second;

	currentTime=hour + ":" + minute + ":" +second;// tim to return
	document.getElementById(idClock).innerHTML = currentTime;//tim for the HTML element

	//recursivity
	if (startTime===undefined) setTimeout("moveClock('"+idClock+"')",timeout);//browser time
	else setTimeout("moveClock('"+idClock+"','"+currentTime+"')",timeout);//set time

}

To use it is really easy:

Normal clock using browser time:

clock

Clock with manual time: (you can write it or use php or asp or whatever you want)

clock

You can also run several clocks in the same site same time 😉

Version: 0.1
Author: Chema Garrido
License: GPL v3
Support: forum.neo22s.com
Notes:
Works in FF 3.5, Chromium, ie6