Repository navigation
Expand file tree
/
Copy pathtimer2.html
More file actions
299 lines (261 loc) · 10.3 KB
/
Copy pathtimer2.html
File metadata and controls
299 lines (261 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>考試倒數計時</title>
<style>
body {
font-family: Arial, sans-serif;
background-image: url('backgroundmou.jpg'); /* 在此放置背景圖片 */
background-size: cover;
color: #333;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}
.container {
background-color: rgba(255, 255, 255, 0.8);
border-radius: 8px;
padding: 20px;
margin: 20px;
text-align: center;
max-width: 600px;
width: 100%;
}
#clock, #exam-info, #countdown, #result {
margin: 20px 0;
}
button {
padding: 10px 20px;
font-size: 1rem;
margin-top: 20px;
cursor: pointer;
}
.exam-item {
margin: 10px 0;
}
input[type="text"], input[type="time"] {
padding: 8px;
margin: 5px 0;
width: 100%;
box-sizing: border-box;
}
</style>
</head>
<body>
<h1>考試倒數計時</h1>
<!-- 考試輸入區域 -->
<div class="container" id="exam-form">
<label for="subject">請輸入考試科目</label>
<input type="text" id="subject" placeholder="科目名稱">
<label for="startTime">開始時間 (24小時制: HH:MM)</label>
<input type="time" id="startTime">
<label for="endTime">結束時間 (24小時制: HH:MM)</label>
<input type="time" id="endTime">
<button onclick="addExam()">新增考試</button>
</div>
<!-- 顯示已輸入的考試列表 -->
<div class="container" id="exam-list"></div>
<!-- 顯示當前時間 -->
<div class="container" id="clock"></div>
<!-- 顯示考試資訊和倒數計時 -->
<div class="container" id="exam-info"></div>
<div class="container" id="countdown"></div>
<!-- 結果顯示 -->
<div class="container" id="result"></div>
<script>
let exams = [];
let currentExamIndex = -1;
let countdownInterval;
let actualEndTime; // 儲存實際結束時間
let examStartTime; // 儲存考試實際開始時間
// 新增考試項目
function addExam() {
const subject = document.getElementById('subject').value.trim();
const startTime = document.getElementById('startTime').value;
const endTime = document.getElementById('endTime').value;
if (subject && startTime && endTime) {
// 計算考試時長(秒)
const [startHour, startMinute] = startTime.split(':').map(Number);
const [endHour, endMinute] = endTime.split(':').map(Number);
const durationSeconds = (endHour * 3600 + endMinute * 60) - (startHour * 3600 + startMinute * 60);
if (durationSeconds <= 0) {
alert('結束時間必須晚於開始時間!');
return;
}
exams.push({ subject, startTime, endTime, duration: durationSeconds, actualDuration: null });
displayExams();
document.getElementById('subject').value = '';
document.getElementById('startTime').value = '';
document.getElementById('endTime').value = '';
} else {
alert('請輸入完整的科目及時間!');
}
}
// 顯示考試列表
function displayExams() {
const examList = document.getElementById('exam-list');
examList.innerHTML = exams.map((exam, index) => `
<div class="exam-item">${index + 1}. ${exam.subject}: ${exam.startTime} - ${exam.endTime}</div>
`).join('');
}
// 程式開始執行
function startProgram() {
if (exams.length === 0) return alert('請先新增考試!');
currentExamIndex = 0;
updateTime();
startExamCountdown();
setInterval(updateTime, 1000); // 每秒更新時間
}
// 更新當前時間
function updateTime() {
const now = new Date();
document.getElementById('clock').innerText = `現在時間:${now.toLocaleTimeString('zh-TW')}`;
}
// 開始考試倒數計時
function startExamCountdown() {
if (currentExamIndex >= exams.length) {
document.getElementById('exam-info').innerText = '恭喜你完成所有考試!';
showFinalResults();
return;
}
const now = new Date();
const currentExam = exams[currentExamIndex];
const [startHour, startMinute] = currentExam.startTime.split(':').map(Number);
const [endHour, endMinute] = currentExam.endTime.split(':').map(Number);
const startTime = new Date(now);
startTime.setHours(startHour, startMinute, 0, 0);
const endTime = new Date(now);
endTime.setHours(endHour, endMinute, 0, 0);
// 如果開始時間已過,假設今天進行考試
if (startTime < now && endTime < now) {
// 調整到明天
startTime.setDate(startTime.getDate() + 1);
endTime.setDate(endTime.getDate() + 1);
}
const examDuration = currentExam.duration; // 以秒計算考試時長
const timeUntilStart = Math.floor((startTime - now) / 1000); // 開始前的等待時間
if (timeUntilStart > 0) {
document.getElementById('exam-info').innerText = `下一場考試:${currentExam.subject},開始時間:${startTime.toLocaleTimeString('zh-TW')}`;
document.getElementById('countdown').innerText = `等待考試開始:${formatTime(timeUntilStart)}`;
// 等待考試開始
countdownInterval = setInterval(() => {
const nowInterval = new Date();
const remaining = Math.floor((startTime - nowInterval) / 1000);
if (remaining <= 0) {
clearInterval(countdownInterval);
startCurrentExam(currentExam, examDuration, startTime);
} else {
document.getElementById('countdown').innerText = `等待考試開始:${formatTime(remaining)}`;
}
}, 1000);
} else {
// 考試已經開始,直接開始倒數
startCurrentExam(currentExam, examDuration, startTime);
}
}
// 開始當前考試的倒數計時
function startCurrentExam(exam, duration, startTime) {
examStartTime = new Date(); // 記錄實際開始時間
document.getElementById('exam-info').innerText = `科目:${exam.subject} - 考試時間:${formatTime(duration)}`;
createStopButton();
countdownInterval = setInterval(() => {
const now = new Date();
const timeElapsed = Math.floor((now - examStartTime) / 1000);
const timeLeft = duration - timeElapsed;
let displayTime;
let isOverTime = false;
let overTime = 0;
// 檢查是否有下一場考試
if (currentExamIndex + 1 < exams.length) {
const nextExam = exams[currentExamIndex + 1];
const [nextStartHour, nextStartMinute] = nextExam.startTime.split(':').map(Number);
const nextStartTime = new Date(now);
nextStartTime.setHours(nextStartHour, nextStartMinute, 0, 0);
// 如果已經到了下一場考試的開始時間,結束當前考試
if (now >= nextStartTime) {
overTime = Math.floor((now - nextStartTime) / 1000);
recordExamResult(timeElapsed, overTime);
clearInterval(countdownInterval);
startExamCountdown();
return;
}
}
if (timeLeft > 0) {
displayTime = formatTime(timeLeft);
} else {
isOverTime = true;
overTime = Math.abs(timeLeft);
displayTime = `超時 ${formatTime(overTime)}`;
}
document.getElementById('countdown').innerText = `倒數:${displayTime}`;
if (isOverTime) {
// 只在下一場考試還未開始的情況下自動結束
if (currentExamIndex + 1 < exams.length) {
// 不自動結束,只顯示超時
// 等待用戶手動停止
} else {
// 最後一場考試,允許超時直到手動停止
}
}
}, 1000);
}
// 倒數計時顯示格式化
function formatTime(seconds) {
const hrs = Math.floor(seconds / 3600).toString().padStart(2, '0');
const mins = Math.floor((seconds % 3600) / 60).toString().padStart(2, '0');
const secs = (seconds % 60).toString().padStart(2, '0');
return `${hrs}:${mins}:${secs}`;
}
// 記錄考試結果
function recordExamResult(timeElapsed, overTime) {
clearInterval(countdownInterval);
actualEndTime = new Date();
// 計算實際用時(秒)
const actualDuration = timeElapsed;
// 記錄實際用時
exams[currentExamIndex].actualDuration = actualDuration;
const resultText = `${exams[currentExamIndex].subject}: 預定 ${formatTime(exams[currentExamIndex].duration)}, 實際 ${formatTime(actualDuration)}`;
document.getElementById('result').innerHTML += `<p>${resultText}</p>`;
removeStopButton();
currentExamIndex++;
}
// 創建停止按鈕
function createStopButton() {
if (!document.getElementById('stopButton')) {
const button = document.createElement('button');
button.id = 'stopButton';
button.innerText = '停止作答';
button.onclick = () => {
clearInterval(countdownInterval);
actualEndTime = new Date(); // 更新實際結束時間為提早交卷時間
const timeElapsed = Math.floor((actualEndTime - examStartTime) / 1000);
const overTime = Math.max(0, timeElapsed - exams[currentExamIndex].duration);
recordExamResult(timeElapsed, overTime);
startExamCountdown();
};
document.body.appendChild(button);
}
}
// 移除停止按鈕
function removeStopButton() {
const button = document.getElementById('stopButton');
if (button) {
button.remove();
}
}
// 顯示所有考試結果
function showFinalResults() {
document.getElementById('result').innerHTML += `<p>今天的考試結果:</p>`;
exams.forEach((exam, index) => {
const scheduledTime = formatTime(exam.duration);
const actualTime = exam.actualDuration !== null ? formatTime(exam.actualDuration) : '未完成';
document.getElementById('result').innerHTML += `<p>${index + 1}. ${exam.subject}: 預定 ${scheduledTime},實際 ${actualTime}</p>`;
});
}
</script>
<button onclick="startProgram()">開始計時</button>
</body>
</html>