###前提・実現したいこと
現在ドットインストールを見ながらストップウォッチを作成中です。しかし、今までうまく動いていたのに、急にボタンを押してもストップウォッチのスタートボタンを押しても秒数が動かなくなってしまいました。
作りかけの中途半端なコードですけれどもわかる方教えてください。
宜しくお願いします。
###該当のソースコード
<html lang="ja"> <head> <meta charset="utf-8"> <title>Stop Watch</title> <style> body{ background: #e0e0e0; font-family: Arial,sans-serif; text-align: center; } #timerText{ color: #00aaff; font-size: 128px; margin: 40px auto; text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5); } </style> </head> <body> <div id="timerText">0.00</div> <button id="start">START</button> <button id="stop">STOP</button> <button id="reset">RESET</button> <script> (function(){ 'use strict'; var startTime; var timerId; var elasedTime = 0; var startButton = document.getElementById('start'); var stopButton = document.getElementById('stop'); var resetButton = document.getElementById('reset'); var timerText = document.getElementById('timerText'); startButton.addEventListener('click',function(){ startTime = Date.now(); // 1970/1/1 00:00:00からの経過ミリ秒 updateTimerText(); }); stopButton.addEventListener('click', function(){ elapsedTime += Date.now() - startTIme; clearTimeout(timerId); }); function updateTimerText() { timerId = setTimeout(function(){ var t = Date.now() - startTime + elapsedTime; timerText.innerHTML = (t/1000).toFixed(2); updateTimerText(); }, 10); } })(); </script> </body> </html>
回答1件
あなたの回答
tips
プレビュー