indexhtml.php <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>Stopwatch</title> <link rel="stylesheet" href="css/styles.css"> <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script> </head> <body> <input type="text" class="text" placeholder="タスク項目"> <div id="timer" name="timer" >000:00.00</div> <button id="start">Start</button> <button id="stop">Stop</button> <button id="reset">Reset</button> <select name="mintue" id = "mintue"> <option value="time_1" id="time1">30</option> <option value="time_2" id="time2">60</option> <option value="time_3" id="time3">90</option> </select> <button id="timeway">Timeway</button> <div id="sum_way" class="sum_way"> </div> <div> <ul id="timers"></ul> <button id="sum">計算</button> <input type="text" id="sum_account"> <button id="php"> php </button> </div> <div id="read_text"></div> <script src="js/main.js"></script> </body> </html> ``` ``` main.js 'use strict'; { const timeoutways = []; 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 minutes = document.getElementById('mintue'); const sum_account = document.getElementById('sum_account'); const timeway = document.getElementById('timeway'); const sum_way = document.getElementById('sum_way'); 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 setButtun () { start.disabled = false; stop.disabled = true; reset.disabled = true; } function setButtunRunning () { start.disabled = true; stop.disabled = false; reset.disabled = true; } function stopButtun () { start.disabled = false; stop.disabled = true; reset.disabled = false; } function calculateRemaining(){ const timeTxt = timer.textContent.replace(":", "").replace(".", ""); const timeresult = minutes.value; switch(timeresult) { case 'time_1': const result1 = String(3000000 - timeTxt); reguler(result1); break; case 'time_2': const result2 = String(6000000 - timeTxt); reguler(result2); break; case 'time_3': const result3 = String(9000000 - timeTxt); reguler(result3); break; } } //正規表現 function reguler(reguler) { const regulation =/(\d{2})(\d{2})(\d{3})/; const timemath = reguler.replace(regulation,'$1分$2秒$3') sum_account.value = timemath; } //途中計算 function way() { const timeTxt = timer.textContent.replace(":", "").replace(".", ""); const regulation2 =/(\d{2})(\d{2})(\d{3})/; const timemath2 = timeTxt.replace(regulation2,'$1分$2秒$3'); timeoutways.push(timemath2); sum_way.innerHTML=`<ul><input>${timeoutways.join("<ul></ul><input>")}</ul>`; }; // ajax 処理 $(function() { $('#php').click(function(){ const timeTxt = timer.textContent.replace(":", "").replace(".", ""); const data = { 'timeattack':timeTxt, } console.log() $("#read_text").load('more.php?timeattack=$(timeTxt)'); }); }); setButtun(); start.addEventListener('click', () => { setButtunRunning(); startTime = Date.now(); countUp(); }); stop.addEventListener('click', () => { stopButtun(); clearTimeout(timeoutId); elapsedTime += Date.now()-startTime; }); reset.addEventListener('click', () => { setButtun(); timer.textContent = '000:00.000'; elapsedTime = 0; }); math.addEventListener('click', () => { stop.click(); calculateRemaining(); }) timeway.addEventListener('click',() =>{ way(); }) } ``` ``` more.php <?php header('Content-type: text/plain; charset= UTF-8'); if(isset($_GET['timeattack'] )){ $timer =$_GET['timeattack']; echo $timer; }else { echo 'こんにちわ'; } ``` 実行したいこと ajaxを使ってphpで時間の表示をechoなど使い表示をする 現状ストップウォッチの時間や計算後の時間を表示したいが値が取れず困っています。 何か解決手段のアドバイスをお願いします。 背景としては今の時間管理ツールは一つのタスクしか時間を表示できないため、他のタスクが来たときに新しく時間管理ができるようにする 例:Aタスク発生→途中の時間の表示を一旦DBやtextに格納→新たにBタスクを開始→Bタスクを終了→途中のAタスクを開始 という流れをしたいです
回答2件
あなたの回答
tips
プレビュー