//JavaScript のタイマークラス化

function timer() {
	this.timeout_sec=2;//デフォルトタイムアウト(秒)
	this.var_timer="タイマー1";//デフォルトタイマー名称
	// タイムアウト処理.
	this.timeoutCheck=function() {
		this.timeout_sec --;
		if(this.timeout_sec <= 0) {
			// タイマーをストップする
			clearInterval(this.timerId);
			//alert( this.var_timer+'が、タイムアウトです。' );
			document.getElementById('span1').innerHTML=this.var_timer+'が、タイムアウトです。';
			return false;
		}
	}
}

function exec_timer(){
	//タイマーのクラスオブジェクトを作る
	var tm=new timer();
	tm.timeout_sec=3;
	tm.timerId =setInterval(function(){tm.timeoutCheck();}, 1000);
	
	var tm2=new timer();//２つ目のタイマーを動かしてみる
	tm2.timeout_sec=1;
	tm2.var_timer="タイマー２";
	tm2.timerId =setInterval(function(){tm2.timeoutCheck();}, 1000);
}


function timer2() {
	this.timeout_sec=2;//デフォルトタイムアウト(秒)
	this.var_timer="タイマー(クラス内コール)";//デフォルトタイマー名称
	//クラスの中から呼ぶ場合
	var self= this;
	this.timerId =setInterval(function(){self.timeoutCheck2();}, 1000);
	//タイムアウト処理
	this.timeoutCheck2=function() {
		this.timeout_sec --;
		if(this.timeout_sec <= 0) {
			// タイマーをストップする
			clearInterval(this.timerId);
			document.getElementById('span2').innerHTML=this.var_timer+'が、タイムアウトです。';
			return false;
		}
	}
}

function exec_timer2(){
	var tm2=new timer2();
	var tm3=new timer2();
	tm3.timeout_sec=1;//タイムアウト時間変更
	tm3.var_timer="タイマー(クラス内コール２)";
}

