|
| 1 | +import pkg from '../package.json' |
| 2 | +import { pausableTimers } from '../src/index' |
| 3 | + |
| 4 | +// 设置页面标题和项目信息 |
| 5 | +document.title = `${pkg.name} | Playground` |
| 6 | +document.querySelector('.project-info h1').textContent = pkg.name |
| 7 | +document.querySelector('.version').textContent = `v${pkg.version}` |
| 8 | + |
| 9 | +const elements = { |
| 10 | + progressBar: document.querySelector('.progress-bar'), |
| 11 | + statusText: document.querySelector('.status-text'), |
| 12 | + timeInput: document.querySelector('#timeInput'), |
| 13 | + modeSelect: document.querySelector('#modeSelect'), |
| 14 | + startBtn: document.querySelector('#startBtn'), |
| 15 | + pauseBtn: document.querySelector('#pauseBtn'), |
| 16 | + clearBtn: document.querySelector('#clearBtn'), |
| 17 | + restartBtn: document.querySelector('#restartBtn'), |
| 18 | +} |
| 19 | + |
| 20 | +let timer = null |
| 21 | +let animationFrameId = null |
| 22 | +let cycleCount = 0 |
| 23 | + |
| 24 | +function updateProgress(remaining, total) { |
| 25 | + const progress = (remaining / total) * 100 |
| 26 | + const progressBar = elements.progressBar |
| 27 | + |
| 28 | + // 完全移除过渡效果 |
| 29 | + progressBar.style.transition = 'none' |
| 30 | + progressBar.style.width = `${progress}%` |
| 31 | + |
| 32 | + if (remaining === total) { |
| 33 | + // 当需要重置到 100% 时,保持 none |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + // 确保 DOM 更新后再添加过渡效果 |
| 38 | + requestAnimationFrame(() => { |
| 39 | + progressBar.style.transition = 'width 0.1s linear' |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +function updateButtons(isRunning) { |
| 44 | + elements.startBtn.disabled = isRunning |
| 45 | + elements.pauseBtn.disabled = !isRunning |
| 46 | + elements.clearBtn.disabled = !isRunning |
| 47 | + elements.restartBtn.disabled = !isRunning |
| 48 | + elements.timeInput.disabled = isRunning |
| 49 | + elements.modeSelect.disabled = isRunning |
| 50 | +} |
| 51 | + |
| 52 | +function animate(total) { |
| 53 | + if (!timer) |
| 54 | + return |
| 55 | + |
| 56 | + const remaining = timer.getRemainingTime() |
| 57 | + updateProgress(remaining, total) |
| 58 | + |
| 59 | + // 根据模式显示不同的状态信息 |
| 60 | + if (elements.modeSelect.value === 'interval') { |
| 61 | + elements.statusText.textContent = `Cycle ${cycleCount} - Remaining: ${remaining}ms` |
| 62 | + } |
| 63 | + else { |
| 64 | + elements.statusText.textContent = `Remaining: ${remaining}ms` |
| 65 | + } |
| 66 | + |
| 67 | + if (timer.isCompleted()) { |
| 68 | + elements.statusText.textContent = 'Completed!' |
| 69 | + updateButtons(false) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + animationFrameId = requestAnimationFrame(() => animate(total)) |
| 74 | +} |
| 75 | + |
| 76 | +function startTimer() { |
| 77 | + const delay = Number.parseInt(elements.timeInput.value) |
| 78 | + // 添加输入值检查 |
| 79 | + if (delay < 100) { |
| 80 | + elements.statusText.textContent = 'Please enter a value greater than 100ms' |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + const mode = elements.modeSelect.value |
| 85 | + |
| 86 | + // 先清理现有的计时器和动画 |
| 87 | + if (timer) { |
| 88 | + timer.clear() |
| 89 | + } |
| 90 | + if (animationFrameId) { |
| 91 | + cancelAnimationFrame(animationFrameId) |
| 92 | + animationFrameId = null |
| 93 | + } |
| 94 | + |
| 95 | + elements.statusText.textContent = '' |
| 96 | + timer = pausableTimers(() => { |
| 97 | + if (mode === 'timeout') { |
| 98 | + cancelAnimationFrame(animationFrameId) |
| 99 | + updateProgress(0, delay) |
| 100 | + elements.statusText.textContent = 'Completed!' |
| 101 | + updateButtons(false) |
| 102 | + } |
| 103 | + else { |
| 104 | + // 在 interval 模式下,先取消动画帧 |
| 105 | + cancelAnimationFrame(animationFrameId) |
| 106 | + // 立即重置进度条到 100%(无动画) |
| 107 | + updateProgress(delay, delay) |
| 108 | + // 在 interval 模式下添加循环次数显示 |
| 109 | + cycleCount++ |
| 110 | + elements.statusText.textContent = `Cycle ${cycleCount}` |
| 111 | + // 立即开始新的倒计时(有动画) |
| 112 | + requestAnimationFrame(() => animate(delay)) |
| 113 | + } |
| 114 | + }, delay, { mode }) |
| 115 | + |
| 116 | + updateButtons(true) |
| 117 | + // 启动时也应用相同的逻辑 |
| 118 | + updateProgress(delay, delay) |
| 119 | + requestAnimationFrame(() => animate(delay)) |
| 120 | +} |
| 121 | + |
| 122 | +// Event Listeners |
| 123 | +elements.startBtn.addEventListener('click', startTimer) |
| 124 | + |
| 125 | +elements.pauseBtn.addEventListener('click', () => { |
| 126 | + if (!timer) |
| 127 | + return |
| 128 | + if (timer.isPaused()) { |
| 129 | + timer.resume() |
| 130 | + elements.pauseBtn.textContent = 'Pause' |
| 131 | + animate(Number.parseInt(elements.timeInput.value)) |
| 132 | + } |
| 133 | + else { |
| 134 | + timer.pause() |
| 135 | + elements.pauseBtn.textContent = 'Resume' |
| 136 | + cancelAnimationFrame(animationFrameId) |
| 137 | + } |
| 138 | +}) |
| 139 | + |
| 140 | +elements.clearBtn.addEventListener('click', () => { |
| 141 | + if (!timer) |
| 142 | + return |
| 143 | + timer.clear() |
| 144 | + // 确保动画帧被取消 |
| 145 | + if (animationFrameId) { |
| 146 | + cancelAnimationFrame(animationFrameId) |
| 147 | + animationFrameId = null |
| 148 | + } |
| 149 | + updateProgress(100, 100) |
| 150 | + elements.statusText.textContent = '' |
| 151 | + updateButtons(false) |
| 152 | + elements.pauseBtn.textContent = 'Pause' |
| 153 | + cycleCount = 0 |
| 154 | +}) |
| 155 | + |
| 156 | +elements.restartBtn.addEventListener('click', () => { |
| 157 | + if (!timer) |
| 158 | + return |
| 159 | + // 先清理现有的动画帧 |
| 160 | + if (animationFrameId) { |
| 161 | + cancelAnimationFrame(animationFrameId) |
| 162 | + animationFrameId = null |
| 163 | + } |
| 164 | + timer.restart() |
| 165 | + elements.pauseBtn.textContent = 'Pause' |
| 166 | + updateProgress(Number.parseInt(elements.timeInput.value), Number.parseInt(elements.timeInput.value)) |
| 167 | + animate(Number.parseInt(elements.timeInput.value)) |
| 168 | + cycleCount = 0 |
| 169 | +}) |
| 170 | + |
| 171 | +// 添加输入框验证 |
| 172 | +elements.timeInput.addEventListener('input', (e) => { |
| 173 | + const value = Number.parseInt(e.target.value) |
| 174 | + if (value < 100) { |
| 175 | + e.target.setCustomValidity('Please enter a value greater than 100ms') |
| 176 | + } |
| 177 | + else { |
| 178 | + e.target.setCustomValidity('') |
| 179 | + } |
| 180 | +}) |
0 commit comments