Repository navigation
Expand file tree
/
Copy pathtimer.html
More file actions
262 lines (230 loc) · 9.02 KB
/
Copy pathtimer.html
File metadata and controls
262 lines (230 loc) · 9.02 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
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>陳鐵蛋專用考試倒數計時器</title>
<style>
body {
font-family: Arial, sans-serif;
background-image: url('background.jpg'); /* 在此放置背景圖片 */
background-size: cover;
color: #333;
display: flex;
flex-direction: column;
align-items: center;
}
.container {
background-color: rgba(255, 255, 255, 0.8);
border-radius: 8px;
padding: 20px;
margin: 20px;
text-align: center;
max-width: 950px;
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;
}
</style>
</head>
<body>
<h1>考試倒數計時</h1>
<!-- 考試輸入區域 -->
<div class="container" id="exam-form">
<label>請輸入考試科目</label>
<input type="text" id="subject" placeholder="科目名稱">
<label>開始時間 (24小時制: HH:MM)</label>
<input type="time" id="startTime">
<label>結束時間 (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 elapsedTime = 0;
let actualEndTime; // 儲存實際結束時間
let examStartTime; // 儲存考試實際開始時間
// 新增考試項目
function addExam() {
const subject = document.getElementById('subject').value;
const startTime = document.getElementById('startTime').value;
const endTime = document.getElementById('endTime').value;
if (subject && startTime && endTime) {
exams.push({ subject, startTime, endTime });
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);
const endTime = new Date(now);
endTime.setHours(endHour, endMinute, 0);
const examDuration = Math.floor((endTime - startTime) / 1000); // 以秒計算考試時長
let timeLeft = Math.max(0, examDuration - Math.floor((now - startTime) / 1000));
elapsedTime = examDuration - timeLeft;
examStartTime = new Date(now); // 記錄實際開始時間
if (now >= startTime) {
document.getElementById('exam-info').innerText = `科目:${currentExam.subject} - 考試時間:${formatTime(examDuration)}`;
countdown(timeLeft, examDuration);
createStopButton(); // 考試開始後顯示停止按鈕
} else {
document.getElementById('exam-info').innerText = `下一場考試:${currentExam.subject}`;
setTimeout(startExamCountdown, (startTime - now)); // 等到考試開始
}
}
// 倒數計時及超時計時
function countdown(timeLeft, duration) {
if (countdownInterval) clearInterval(countdownInterval);
countdownInterval = setInterval(() => {
const now = new Date();
document.getElementById('clock').innerText = `現在時間:${now.toLocaleTimeString('zh-TW')}`;
let remainingSeconds = timeLeft - elapsedTime;
const overTime = remainingSeconds < 0 ? -remainingSeconds : 0;
let displayTime;
if (remainingSeconds > 0) {
displayTime = formatTime(remainingSeconds);
} else {
displayTime = `超時 ${formatTime(overTime)}`;
}
document.getElementById('countdown').innerText = `倒數:${displayTime}`;
elapsedTime++;
if (remainingSeconds <= -duration) {
clearInterval(countdownInterval);
recordExamResult(duration, overTime);
}
}, 1000);
}
// 格式化時間(秒數轉換為 HH:MM:SS)
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(duration, overTime) {
const totalTime = duration + overTime;
const exam = exams[currentExamIndex];
const actualDuration = Math.floor((actualEndTime - examStartTime) / 1000); // 計算實際考試時長
const result = `${exam.subject}: 預定 ${formatTime(duration)},實際 ${formatTime(actualDuration)}`;
document.getElementById('result').innerHTML += `<p>${result}</p>`;
removeStopButton();
currentExamIndex++;
if (currentExamIndex < exams.length) {
startRestPeriod();
} else {
document.getElementById('exam-info').innerText = '恭喜你完成所有考試!';
showFinalResults();
}
}
// 休息時間倒數計時
function startRestPeriod() {
const nextExamStart = new Date();
const [nextStartHour, nextStartMinute] = exams[currentExamIndex].startTime.split(':').map(Number);
nextExamStart.setHours(nextStartHour, nextStartMinute, 0);
let restTimeLeft = Math.floor((nextExamStart - actualEndTime) / 1000); // 休息時間以秒計
if (restTimeLeft <= 0) {
// 若休息時間已過,直接進入下一場考試
startExamCountdown();
} else {
document.getElementById('exam-info').innerText = `休息 ${formatTime(restTimeLeft)}`;
countdownInterval = setInterval(() => {
const now = new Date();
document.getElementById('clock').innerText = `現在時間:${now.toLocaleTimeString('zh-TW')}`;
document.getElementById('countdown').innerText = `休息倒數:${formatTime(restTimeLeft)}`;
restTimeLeft--;
if (restTimeLeft < 0 || new Date() >= nextExamStart) {
clearInterval(countdownInterval);
startExamCountdown(); // 開始下一場考試
}
}, 1000);
}
}
// 創建停止按鈕
function createStopButton() {
if (!document.getElementById('stopButton')) {
const button = document.createElement('button');
button.id = 'stopButton';
button.innerText = '停止作答';
button.onclick = () => {
clearInterval(countdownInterval);
actualEndTime = new Date(); // 更新實際結束時間為提早交卷時間
recordExamResult(elapsedTime, Math.max(0, elapsedTime - exams[currentExamIndex].duration));
};
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) => {
document.getElementById('result').innerHTML += `<p>${index + 1}. ${exam.subject}: 預定 ${formatTime(exam.duration)},實際 ${formatTime(exam.actualDuration)}</p>`;
});
}
</script>
<button onclick="startProgram()">開始計時</button>
</body>
</html>