以下是一个简单的 JavaScript 实时监控例子,每秒钟更新一次时间,并在页面上显示。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>JS Watch Example</title>
  </head>
  <body>
    <h1 id="time"></h1>
    <script>
      function updateTime() {
        var now = new Date();
        var hours = now.getHours();
        var minutes = now.getMinutes();
        var seconds = now.getSeconds();
        var timeString = hours + ':' + minutes + ':' + seconds;
        document.getElementById('time').innerText = timeString;
      }
      setInterval(updateTime, 1000); // 每秒钟更新时间
    </script>
  </body>
</html>

在这个例子中,我们使用了 setInterval 函数来每秒钟调用一次 updateTime 函数。这个函数获取当前时间并将其格式化为一个字符串,然后将其设置为页面上的 h1 元素的文本内容。这样,每秒钟,文本内容就会被更新,从而实现了实时监控的效果

js watch 例子

原文地址: http://www.cveoy.top/t/topic/cZ6c 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录