html
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="utf-8"> 5 <title>Stopwatch</title> 6 <link rel="stylesheet" href="styles.css"> 7 8</head> 9<body> 10 <div id="timer">00:00.000</div> 11 <button id="start">Start</button> 12 <button id="stop">Stop</button> 13 <button id="reset">Reset</button> 14 <select name="mintus" id ="mintus" class="mintus"> 15 <option value="time_1" id="time1">30</option> 16 <option value="time_2" id="time2">60</option> 17 <option value="time_3" id="time3">90</option> 18 </select> 19 <div> 20 <button id="sum">計算</button> 21 <div id="sum_account" ></div> 22 </div> 23 24 25 <script src="main.js"></script> 26</body> 27</html>
JavaScript
1'use strict'; 2 3{ 4 const timer = document.getElementById('timer'); 5 const start = document.getElementById('start'); 6 const stop = document.getElementById('stop'); 7 const reset = document.getElementById('reset'); 8 const mintus = document.getElementById('mintus'); 9 const math = document.getElementById('sum'); 10 const sum_account = document.getElementById('sum_account'); 11 12 let startTime; 13 let timeoutId; 14 let elapsedTime = 0; 15 16 17 function countUp() { 18 const d = new Date(Date.now() - startTime + elapsedTime); 19 const m = String(d.getMinutes()).padStart(2, '0'); 20 const s = String(d.getSeconds()).padStart(2, '0'); 21 const ms = String(d.getMilliseconds()).padStart(3, '0'); 22 timer.textContent = `${m}:${s}.${ms}`; 23 24 timeoutId = setTimeout(() => { 25 countUp(); 26 }, 10); 27 } 28 29 function setButtonStateInitial() { 30 start.disabled = false; 31 stop.disabled = true; 32 reset.disabled = true; 33 } 34 35 function setButtonStateRunning() { 36 start.disabled = true; 37 stop.disabled = false; 38 reset.disabled = true; 39 } 40 41 function setButtonStateStopped() { 42 start.disabled = false; 43 stop.disabled = true; 44 reset.disabled = false; 45 } 46 47 // 残り時間の計算 48 function calculateRemaining(timeTxt) { 49 const timeresult = mintus.value; 50 switch(timeresult) { 51 case 'time_1': 52 const result1 = 3000000 - timeTxt; 53 54 sum_account.innerHTML = result1; 55 break; 56 case 'time_2': 57 const result2 = 6000000 - timeTxt; 58 sum_account.innerHTML = result2; 59 break; 60 case 'time_3': 61 const result3 = 9000000 - timeTxt; 62 sum_account.innerHTML = result3; 63 break; 64 } 65 } 66 67 68 69 setButtonStateInitial(); 70 71 start.addEventListener('click', () => { 72 setButtonStateRunning(); 73 startTime = Date.now(); 74 countUp(); 75 }); 76 77 stop.addEventListener('click', () => { 78 setButtonStateStopped(); 79 clearTimeout(timeoutId); 80 elapsedTime += Date.now() - startTime; 81 }); 82 83 reset.addEventListener('click', () => { 84 setButtonStateInitial(); 85 timer.textContent = '00:00.000'; 86 elapsedTime = 0; 87 }); 88 89 90 math.addEventListener('click', () => { 91 let timeTxt = timer.textContent.replace(":", "").replace(".", ""); 92 stop.click(); 93 calculateRemaining(timeTxt); 94 }); 95}
計算結果の表示を〇〇分〇〇秒、〇〇という表示にしたいです。
何か知見をお教えくださいよろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/26 13:44
2020/09/26 14:06
2020/09/26 14:16