index.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 <form name ="form"> 15 <select name="mintus" class="mintus"> 16 <option value="time_1" id="time1">30</option> 17 <option value="time_2" id="time2">60</option> 18 <option value="time_3" id="time3">90</option> 19 </select> 20 </form> 21 <div> 22 <button id="sum">計算</button> 23 <input type-text id="sum_account"> 24 </div> 25 26 <script src="main.js"></script> 27</body> 28</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 math = document.getElementById('sum'); 9 const mintus = document.getElementById('mintus'); 10 const timeselect = document.querySelector('.mintus') 11 12 let startTime; 13 let timeoutId; 14 let elapsedTime = 0; 15 16 function countUp() { 17 const d = new Date(Date.now() - startTime + elapsedTime); 18 const m = String(d.getMinutes()).padStart(2, '0'); 19 const s = String(d.getSeconds()).padStart(2, '0'); 20 const ms = String(d.getMilliseconds()).padStart(3, '0'); 21 timer.textContent = `${m}:${s}.${ms}`; 22 23 timeoutId = setTimeout(() => { 24 countUp(); 25 }, 10); 26 } 27 28 function setButtonStateInitial() { 29 start.disabled = false; 30 stop.disabled = true; 31 reset.disabled = true; 32 } 33 34 function setButtonStateRunning() { 35 start.disabled = true; 36 stop.disabled = false; 37 reset.disabled = true; 38 } 39 40 function setButtonStateStopped() { 41 start.disabled = false; 42 stop.disabled = true; 43 reset.disabled = false; 44 } 45 46 setButtonStateInitial(); 47 48 start.addEventListener('click', () => { 49 setButtonStateRunning(); 50 startTime = Date.now(); 51 countUp(); 52 }); 53 54 stop.addEventListener('click', () => { 55 setButtonStateStopped(); 56 clearTimeout(timeoutId); 57 elapsedTime += Date.now() - startTime; 58 }); 59 60 reset.addEventListener('click', () => { 61 setButtonStateInitial(); 62 timer.textContent = '00:00.000'; 63 elapsedTime = 0; 64 }); 65 66 67 math.addEventListener('click', () => { 68 stop.click(); 69 let timeTxt = timer.textContent.replace(":", "").replace(".", ""); 70 // let timeresult = "#time1"||"#time2"||"#time3"; 71 // let timeresult = str; 72 selectElement.addEventListener('change',(event) =>{ 73 const timeresult = timeselect; 74 }); 75 switch (timeresult) { 76 case "time_1": 77 const a =180000 - timeTxt; 78 console.log(a) 79 break; 80 case "time_2": 81 const b =360000 - timeTxt; 82 console.log(b) 83 break; 84 case "time_3": 85 const c =540000 - timeTxt; 86 console.log(c); 87 break; 88 } 89}
実装したいこととしては
1 math.addEventListener('click', () => {以降の部分で、htmlのoptionが変更されるたびにその値をJavaScriptのswitch構文の式に当てはめたい
changeメソッドを使うことはわかるが、その記載方法がわからず、selectElement.addEventListener('change',(event以下の記述で timeresultに選択した選択した値を入れるような実装したい
説明が下手ですいません。よろしければご意見をお願いします。
回答3件
あなたの回答
tips
プレビュー