body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
.clock-container {
text-align: center;
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.time-display {
font-size: 2em;
margin-bottom: 20px;
}
let alarmTime = null;
const alarmSound = new Audio('alarm-sound.mp3');
function startClock() {
setInterval(updateClock, 1000); // 每秒更新一次时钟
}
function updateClock() {
const now = new Date();
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
const seconds = now.getSeconds().toString().padStart(2, '0');
document.getElementById('current-time').textContent = `${hours}:${minutes}:${seconds}`;
}
function setAlarm() {
const input = document.getElementById('alarm-time').value;
if (input) {
alarmTime = input;
alert(`Alarm set for ${alarmTime}`);
}
}
function checkAlarm() {
const now = new Date();
const currentTime = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`;
if (currentTime === alarmTime) {
alarmSound.play();
alert('Time to wake up!');
alarmTime = null; // 重置闹钟时间
}
}
startClock();
setInterval(checkAlarm, 1000); // 每秒检查一次闹钟