js网页整点报时。
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>整点播报时间</title> </head> <body> <h1>整点播报时间</h1> <!-- 点击按钮立即播报当前时间 --> <button onclick="reportCurrentTime()">播报当前时间</button> <script> // 播报时间的函数 function speak(text) { const utterance = new SpeechSynthesisUtterance(text); utterance.lang = 'zh-CN'; // 设置播报语言为中文 window.speechSynthesis.speak(utterance); } // 获取当前时间并播报 function reportTime() { const now = new Date(); const hour = now.getHours(); const minute = now.getMinutes(); // 只有在整点时(分钟为0)播报 if (minute === 0) { let timeText = `现在是${hour}点整`; speak(timeText); } } // 点击按钮播报当前时间 function reportCurrentTime() { const now = new Date(); const hour = now.getHours(); const minute = now.getMinutes(); let timeText = `现在是${hour}点${minute}分`; speak(timeText); } // 设置每分钟检查一次时间 setInterval(reportTime, 60000); // 页面加载时立即检查一次时间 reportTime(); </script> </body> </html>