JavaScript (HTML5): 時計

clock

clock.js

(function() {
	function Clock() {
		var that = this;
		this.timer = setInterval(function() {
			that.tick();
		}, 1000);
		
		this.timerText = document.createTextNode("");
		document.body.appendChild(this.timerText);
		this.tick();
	};
	Clock.prototype.tick = function() {
		var date = new Date();
		
		this.yyyy = date.getFullYear();
		this.MM = date.getMonth() + 1;
		this.dd = date.getDate();
		this.hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
		this.mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
		this.ss = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
		
		this.timerText.nodeValue = this.yyyy + "/" + this.MM + "/" + this.dd + " " + this.hh + ":" + this.mm + ":" + this.ss;
	};
	function loaded() {
		window.clock = new Clock();
	}
	
	if(document.addEventListener){
		document.addEventListener("DOMContentLoaded", loaded, false);
	} else if(/msie/.test(navigator.userAgent.toLowerCase())){
		try {
			document.documentElement.doScroll("left");
		} catch(error){
			setTimeout(arguments.callee, 0);
			return;
		}
		loaded();
	}
}).call(this);