jsで独立したストップウォッチを複数作りたい
解決済
回答 1
投稿 ・編集
- 評価
- クリップ 0
- VIEW 3,391
一つだけならできたのですが、独立したストップウォッチを複数作るとなると、
不具合が起きてしまいます。
独立した複数のストップウォッチの作り方を教えてください。
よろしくお願いします。
(以下は単体のストップウォッチのコードです)
<h1>ストップウォッチ</h1>
<div id="sec" style="font-size:128px">0.00</div>
<input type="button" value="start!" id="run">
<input type="button" value="stop!" id="stop">
<input type="button" value="reset!" id="reset">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(function(){
var startTime,
timerId,
running = false,
stopTime;
$('#run').click(function(){
run();
clickRun();
});
$('#stop').click(function(){
stop();
clickstop();
});
$('#reset').click(function(){
reset();
clickReset();
});
function run(){
if(running) return;
running = true;
if (stopTime){
startTime = startTime + (new Date()).getTime() - stopTime;
}
if(!startTime){
startTime = (new Date()).getTime();
}
timer();
}
function timer(){
document.getElementById('sec').innerHTML = (((new Date()).getTime() - startTime) / 1000).toFixed(2);
timerId = setTimeout(function(){
timer();
},100);
}
function stop(){
if(!running) return false;
running = false;
clearTimeout(timerId);
stopTime = (new Date()).getTime();
}
function reset(){
if(running) return;
startTime = undefined;
document.getElementById('sec').innerHTML = '0.00';
}
function clickRun(){
$('#run').css('display','none');
$('#stop').show();
$('#reset').show();
}
function clickstop(){
$('#run').show();
$('#stop').css('display','none');
}
function clickReset(){
$('#run').show();
$('#stop').css('display','none');
$('#reset').css('display','none');
}
});
</script>
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
+3
http://jsdo.it/utun001/lTD7
※ jQuery使ってます。
<div id="sw1">
<button class="startBtn">start</button>
<button class="stopBtn">stop</button>
<button class="resetBtn">reset</button>
<div class="timerText">0</div>
</div>
<div id="sw2">
<button class="startBtn">start</button>
<button class="stopBtn">stop</button>
<button class="resetBtn">reset</button>
<div class="timerText">0</div>
</div>
<div id="sw3">
<button class="startBtn">start</button>
<button class="stopBtn">stop</button>
<button class="resetBtn">reset</button>
<div class="timerText">0</div>
</div>
//クラス作成&コンストラクタ
var StopWatch = function(_continerId){
var self = this;
this.continerSelecter = '#'+_continerId;
this.startBtnSelecter = '.startBtn';
this.stopBtnSelecter = '.stopBtn';
this.resetBtnSelecter = '.resetBtn';
this.timerTextSelecter = '.timerText';
this.defaultInterval = 1000;
this.timerId = null;
// クリックイベント登録
$(this.continerSelecter+'>'+this.startBtnSelecter).click(function(){
self.start();
});
$(this.continerSelecter+'>'+this.stopBtnSelecter).click(function(){
self.stop();
});
$(this.continerSelecter+'>'+this.resetBtnSelecter).click(function(){
self.reset();
});
};
//各function
StopWatch.prototype.start = function(){
this.run();
};
StopWatch.prototype.stop = function(){
if(this.timerId !== null){
clearInterval(this.timerId);
}
};
StopWatch.prototype.reset = function(){
$(this.continerSelecter+'>'+this.timerTextSelecter).text('0');
};
StopWatch.prototype.run = function(){
var self = this;
this.timerId = setInterval(function(){
var num = $(self.continerSelecter+'>'+self.timerTextSelecter).text();
$(self.continerSelecter+'>'+self.timerTextSelecter).text(parseInt(num,10)+1);
}, this.defaultInterval);
};
// main処理
$(document).ready(function(){
var sw1 = new StopWatch('sw1');
var sw2 = new StopWatch('sw2');
var sw3 = new StopWatch('sw3');
});
こんな感じで如何でしょうか。
ストップウォッチクラスを作って、インスタンス生成してから処理する様にすれば、いくつでも作れるようになりますよ。
以上、ご参考になれば幸いです。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.19%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2015/01/16 20:54
ただ、不具合が多いので、手直ししていただけると幸いです。
どうか、よろしくお願いします。
2015/01/20 13:03