實作一個簡單的計時器,包含開始 / 暫停 / 重置功能,並透過這題練習:
- template 資料顯示
- event handler 綁定
ref/reactive使用- HTML attributes 控制(disabled / class / style)
頁面需包含:
- 一個顯示時間的區塊(格式:
00:00:00) - 一個
Start按鈕 - 一個
Stop按鈕 - 一個
Reset按鈕
- 初始時間:
00:00:00 - 每秒 +1 秒
- 格式為:
HH:mm:ss - 不需要處理超過 24 小時
- 開始計時(使用
setInterval) - 若已在計時中,不可重複啟動
- 暫停計時(保留當前時間)
- 清除計時
- 回到
00:00:00 - 停止計時
請實作:
- 計時是否進行中(
isRunning) - 秒數(或時間物件)
必須使用:
setup() (or <script setup>)
ref()
或 reactive()需包含:
{{ }}顯示時間@click綁定事件
需實作:
| 按鈕 | 條件 |
|---|---|
| Start | 計時中 → disabled |
| Stop | 未計時 → disabled |
| Reset | 永遠可用 |
👉 必須使用 :disabled 綁定
例如:
- 計時中 → 顯示綠色
- 停止時 → 顯示灰色
👉 使用:
:class="..."
或
:style="..."可以只存秒數:
const seconds = ref(0)let timerId: number | null = nullfunction formatTime(totalSeconds: number): string- 重複按 Start 不應該開多個 interval
- 按鈕顯示不同文字(例如 Start → Running)
- 或加 icon(非必要)
- 把
formatTime、startTimer、stopTimer、resetTimer拆成清楚的 function
watchcomputed- 元件拆分(例如
TimerDisplay.vue、TimerControl.vue)
- ❌ 不可使用 Options API
- ❌ 不可直接操作 DOM(例如
document.getElementById) - ❌ 不可使用第三方 timer 套件