ソースコード
indexhtml
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 15 <select name="mintus"> 16 <option value="time_1">30</option> 17 <option value="time_2">60</option> 18 <option value="time_3">90</option> 19 </select> 20 <div> 21 <button id="sum">計算</button> 22 <input type-text id="sum_account"> 23 </div> 24 25 <script src="main.js"></script> 26</body> 27</html>
main.js 'use strict'; { const timer = document.getElementById('timer'); const start = document.getElementById('start'); const stop = document.getElementById('stop'); const reset = document.getElementById('reset'); const math = document.getElementById('sum'); const mintus = document.getElementById('mintus'); let startTime; let timeoutId; let elapsedTime = 0; function countUp() { const d = new Date(Date.now() - startTime + elapsedTime); const m = String(d.getMinutes()).padStart(2, '0'); const s = String(d.getSeconds()).padStart(2, '0'); const ms = String(d.getMilliseconds()).padStart(3, '0'); timer.textContent = `${m}:${s}.${ms}`; timeoutId = setTimeout(() => { countUp(); }, 10); } function setButtonStateInitial() { start.disabled = false; stop.disabled = true; reset.disabled = true; } function setButtonStateRunning() { start.disabled = true; stop.disabled = false; reset.disabled = true; } function setButtonStateStopped() { start.disabled = false; stop.disabled = true; reset.disabled = false; } setButtonStateInitial(); start.addEventListener('click', () => { setButtonStateRunning(); startTime = Date.now(); countUp(); }); stop.addEventListener('click', () => { setButtonStateStopped(); clearTimeout(timeoutId); elapsedTime += Date.now() - startTime; }); reset.addEventListener('click', () => { setButtonStateInitial(); timer.textContent = '00:00.000'; elapsedTime = 0; }); math.addEventListener('click',() => { var fact = timer.getTime() - mintus.getTime(); fact(); console.log(fact); }) }
実現したいこと
bodyのtimerの時間とselecteの値を選択した値の計算をし、その値をだす
試したこと
const math = document.getElementById('sum');
const mintus = document.getElementById('mintus');と変数に起き
var fact = timer.getTime() - mintus.getTime();
timerの値とselectの値を取得するmintusの差を表示する
補足情報(FW/ツールのバージョンなど)
言語 JavaScript sql sequel pro
コード VScode
回答1件
あなたの回答
tips
プレビュー