Sep
14
2024
js网页整点报时
作者:
绝缘体.. 发布:
2024-09-14 08:36 分类:
未分类 阅读:
抢沙发
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>
微信扫一扫,打赏作者吧~