|
| 1 | +let isRunning = false; |
| 2 | +let timerInterval = null; |
| 3 | +// timer.js |
| 4 | +const MODES = { |
| 5 | + work: { label: 'Work', seconds: 25 * 60 }, |
| 6 | + short: { label: 'Short Break', seconds: 5 * 60 }, |
| 7 | + long: { label: 'Long Break', seconds: 15 * 60 } |
| 8 | +}; |
| 9 | +let currentMode = 'work'; |
| 10 | +let remainingSeconds = MODES[currentMode].seconds; |
| 11 | +let sessionCount = 0; |
| 12 | +const SESSIONS_PER_SET = 4; |
| 13 | + |
| 14 | +function updateTimerDisplay() { |
| 15 | + const min = String(Math.floor(remainingSeconds / 60)).padStart(2, '0'); |
| 16 | + const sec = String(remainingSeconds % 60).padStart(2, '0'); |
| 17 | + document.getElementById('timer').textContent = `${min}:${sec}`; |
| 18 | + document.getElementById('mode-label').textContent = MODES[currentMode].label; |
| 19 | + document.getElementById('session-count').textContent = sessionCount; |
| 20 | + // 円形進捗バー更新 |
| 21 | + const circle = document.getElementById('progress-circle'); |
| 22 | + const r = 100; |
| 23 | + const c = 2 * Math.PI * r; |
| 24 | + circle.setAttribute('stroke-dasharray', c); |
| 25 | + let total = MODES[currentMode].seconds; |
| 26 | + let percent = (total - remainingSeconds) / total; |
| 27 | + // stroke-dashoffsetで進捗を表現(0で空、cで全塗り) |
| 28 | + circle.setAttribute('stroke-dashoffset', (1 - percent) * c); |
| 29 | + // 残り時間が0未満にならないように |
| 30 | + if (remainingSeconds < 0) { |
| 31 | + document.getElementById('timer').textContent = '00:00'; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +function startTimer() { |
| 37 | + if (isRunning) return; |
| 38 | + isRunning = true; |
| 39 | + timerInterval = setInterval(() => { |
| 40 | + if (remainingSeconds > 0) { |
| 41 | + remainingSeconds--; |
| 42 | + updateTimerDisplay(); |
| 43 | + } else { |
| 44 | + clearInterval(timerInterval); |
| 45 | + isRunning = false; |
| 46 | + if (currentMode === 'work') { |
| 47 | + sessionCount++; |
| 48 | + updateTimerDisplay(); |
| 49 | + } |
| 50 | + notifyTimerEnd(); |
| 51 | + } |
| 52 | + }, 1000); |
| 53 | +function notifyTimerEnd() { |
| 54 | + // Web通知 |
| 55 | + if (window.Notification && Notification.permission === 'granted') { |
| 56 | + new Notification('タイマー終了', { body: MODES[currentMode].label + 'タイムが終了しました!' }); |
| 57 | + } else if (window.Notification && Notification.permission !== 'denied') { |
| 58 | + Notification.requestPermission().then(permission => { |
| 59 | + if (permission === 'granted') { |
| 60 | + new Notification('タイマー終了', { body: MODES[currentMode].label + 'タイムが終了しました!' }); |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + // アラート音(ローカルファイルを使用) |
| 65 | + const beep = new Audio('beep.mp3'); |
| 66 | + // 読み込みエラーのハンドリング |
| 67 | + beep.addEventListener('error', (event) => { |
| 68 | + console.error('アラート音の読み込みに失敗しました:', event); |
| 69 | + }); |
| 70 | + // 再生時エラーのハンドリング(対応ブラウザでは Promise を利用) |
| 71 | + const playResult = beep.play(); |
| 72 | + if (playResult && typeof playResult.catch === 'function') { |
| 73 | + playResult.catch((error) => { |
| 74 | + console.error('アラート音の再生に失敗しました:', error); |
| 75 | + }); |
| 76 | + } |
| 77 | +} |
| 78 | +} |
| 79 | + |
| 80 | +function pauseTimer() { |
| 81 | + if (timerInterval) { |
| 82 | + clearInterval(timerInterval); |
| 83 | + isRunning = false; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +function resetTimer() { |
| 89 | + pauseTimer(); |
| 90 | + remainingSeconds = MODES[currentMode].seconds; |
| 91 | + updateTimerDisplay(); |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +function switchMode(mode) { |
| 96 | + if (!MODES[mode]) return; |
| 97 | + // タイマー実行中にモードを切り替える場合はユーザーに確認する |
| 98 | + if (isRunning) { |
| 99 | + const confirmed = window.confirm('タイマーが実行中です。モードを切り替えると現在のタイマーがリセットされます。続行しますか?'); |
| 100 | + if (!confirmed) { |
| 101 | + return; |
| 102 | + } |
| 103 | + } |
| 104 | + currentMode = mode; |
| 105 | + resetTimer(); |
| 106 | +} |
| 107 | + |
| 108 | +document.addEventListener('DOMContentLoaded', () => { |
| 109 | + document.getElementById('start-btn').addEventListener('click', startTimer); |
| 110 | + document.getElementById('pause-btn').addEventListener('click', pauseTimer); |
| 111 | + document.getElementById('reset-btn').addEventListener('click', resetTimer); |
| 112 | + document.getElementById('work-btn').addEventListener('click', () => switchMode('work')); |
| 113 | + document.getElementById('short-break-btn').addEventListener('click', () => switchMode('short')); |
| 114 | + document.getElementById('long-break-btn').addEventListener('click', () => switchMode('long')); |
| 115 | + updateTimerDisplay(); |
| 116 | + // 通知許可リクエスト |
| 117 | + if (window.Notification && Notification.permission !== 'granted') { |
| 118 | + Notification.requestPermission(); |
| 119 | + } |
| 120 | +}); |
0 commit comments