js网页整点报时

 
更多

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>
打赏

本文固定链接: https://www.cxy163.net/archives/2928 | 绝缘体

该日志由 绝缘体.. 于 2024年09月14日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: js网页整点报时 | 绝缘体
关键字: , , , ,

js网页整点报时:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter