私は1行に1つずつプロセスをリストしたhtmlテーブルを持っています。私は、それぞれのプロセスの経過時間を示すために、それぞれの行に別々のタイマーを追加したいと思います。setTimeout()タイマーを停止するのに助けが必要
プロセスが完了したらタイマーを停止し、プロセスがその長さで実行されると2時間後に自動的にタイマーを停止する必要があります。
以下のコードでは複数のタイマーを実行できますが、個々のタイマーを停止する関数や、2つの実行時間後にタイマーを停止する関数を作成する方法はわかりません。
誰もがこれを手伝ってくれますか?
おかげで、
ジェフ
<html>
<head>
<script type="text/javascript">
function timer(elementId){
this.startTime = new Date();
this.hours = 0, this.minutes = 0, this.seconds = 0, this.borrowed = 0;
this.update(elementId);
}
timer.prototype.getDifference=function(start,now,MAX){
var diff = now - start - this.borrowed;
this.borrowed = 0;
if (diff > -1) return diff;
this.borrowed = 1;
return (MAX + diff);
}
timer.prototype.timerPad=function(){
this.seconds = this.addZero(this.seconds);
this.minutes = this.addZero(this.minutes);
this.hours = this.addZero(this.hours);
}
timer.prototype.addZero=function(value){
return value < 10 ? ("0" + value) : value;
}
timer.prototype.update=function(elementId){
var currTime = new Date();
var startTime = this.startTime;
this.seconds = this.getDifference(startTime.getSeconds(), currTime.getSeconds(), 60);
this.minutes = this.getDifference(startTime.getMinutes(), currTime.getMinutes(), 60);
this.hours = this.getDifference(startTime.getHours(), currTime.getHours(), 2);
this.timerPad();
var e = document.getElementById(elementId);
e.innerHTML = this.hours + ":" + this.minutes + ":" + this.seconds;
var self = this;
this.timer = setTimeout(function(){self.update(elementId);},1000);
}
</script>
</head>
<body>
<button type="button" onClick="javascript:new timer('timer1');">Start!</button>
<button type="button" onClick="">Stop!</button>
<div id="timer1"></div><p>
<button type="button" onClick="javascript:new timer('timer2');">Start!</button>
<button type="button" onClick="">Stop!</button>
<div id="timer2"></div>
</body>
</html>
これは完全に機能しました!それはまさに私が達成しようとしていたものですが、何日もつまずきます。助けてくれてありがとう。今私は2時間後に自動停止する方法を理解する必要があり、私はすべて設定されています。再度、感謝します! – Jeff
うれしかったです。 2時間後にポストを編集してタイマーのオートストップを作成しました。 – Chandu
それは素晴らしいです!もう一度ありがとう....私は本当にこれで苦労しています。 – Jeff