点击开始按钮后显示当前时间值

所以我有一个工作正常的时钟。但我现在要显示/打印的是单击开始 rit 后该时钟中的当前时间。我试图首先停止设置计数器,但这也停止了我脚本第一部分中的计时器。现在发生的事情是我只有两个计时器。我不想要那个,我想点击开始 rit 按钮,然后它需要当前的时间,然后在 txt 中打印它。


    

      function ItsShowTime(){

        var date = new Date();

        var h = date.getHours();

        var m = date.getMinutes();

        var s = date.getSeconds();

        if(h == 0){

        }

        h = (h < 10) ? "0" + h : h;

        m = (m < 10) ? "0" + m : m;

        s = (s < 10) ? "0" + s : s;

        var time = h + ":" + m + ":" + s;



        document.getElementById("Clock").innerText = time;

        document.getElementById("Clock").textContent = time;

        setTimeout(ItsShowTime, 1000);

        CurrentTime(time);

      }

      ItsShowTime();

      function CurrentTime(time){

          document.getElementById("txt").innerText = time;

      }

<!DOCTYPE html>

<html dir="ltr">

  <head>

    <meta charset="utf-8">

    <title>Digital Clock</title>

  </head>

  <body>

    <input type="button" value="Start rit" onclick="CurrentTime()">

    <div id="txt"></div>

    <div id="Clock">



    </div>

  </body>

</html>

我检查了 w3schools 怎么做我检查了 Bitdegree 和堆栈上的几个类似问题,但我找不到我需要的确切内容


慕容森
浏览 154回答 3
3回答

月关宝盒

Javascript代码&nbsp; &nbsp; &nbsp; &nbsp; var timer = undefined;&nbsp; &nbsp; &nbsp; &nbsp; function getShowTime() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var date = new Date();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var h = date.getHours();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var m = date.getMinutes();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var s = date.getSeconds();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (h == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; h = (h < 10) ? "0" + h : h;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m = (m < 10) ? "0" + m : m;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = (s < 10) ? "0" + s : s;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return h + ":" + m + ":" + s;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function CurrentTime() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (timer) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clearTimeout(timer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer = setTimeout(() => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const time = getShowTime();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("txt").innerText = time;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, 1000);&nbsp; &nbsp; &nbsp; &nbsp; }和 HTML 代码<!DOCTYPE html><html dir="ltr">&nbsp; <head>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <title>Digital Clock</title>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <input type="button" value="Start rit" onclick="CurrentTime()">&nbsp; &nbsp; <div id="txt"></div>&nbsp; &nbsp; <div id="Clock">&nbsp; &nbsp; </div>&nbsp; </body></html>

慕丝7291255

如果您想每秒自动刷新一次,请使用:<!DOCTYPE html><html dir="ltr"><head>&nbsp; <meta charset="utf-8">&nbsp; <title>Digital Clock</title></head><body>&nbsp; <input type="button" value="Start rit" onclick="ItsShowTime()">&nbsp; <div id="txt"></div>&nbsp; <div id="Clock">&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; function ItsShowTime() {&nbsp; &nbsp; &nbsp; &nbsp; var date = new Date();&nbsp; &nbsp; &nbsp; &nbsp; var h = date.getHours();&nbsp; &nbsp; &nbsp; &nbsp; var m = date.getMinutes();&nbsp; &nbsp; &nbsp; &nbsp; var s = date.getSeconds();&nbsp; &nbsp; &nbsp; &nbsp; if (h == 0) {}&nbsp; &nbsp; &nbsp; &nbsp; h = (h < 10) ? "0" + h : h;&nbsp; &nbsp; &nbsp; &nbsp; m = (m < 10) ? "0" + m : m;&nbsp; &nbsp; &nbsp; &nbsp; s = (s < 10) ? "0" + s : s;&nbsp; &nbsp; &nbsp; &nbsp; var time = h + ":" + m + ":" + s;&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(ItsShowTime, 1000);&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("txt").innerText = time;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; </script>&nbsp; </div></body></html>如果不只是删除 setTimeout();

慕尼黑的夜晚无繁华

如果你把它们分成两个单独的函数,这样会更容易:var time;function ItsShowTime(){&nbsp; &nbsp; &nbsp; &nbsp; var date = new Date();&nbsp; &nbsp; &nbsp; &nbsp; var h = date.getHours();&nbsp; &nbsp; &nbsp; &nbsp; var m = date.getMinutes();&nbsp; &nbsp; &nbsp; &nbsp; var s = date.getSeconds();&nbsp; &nbsp; &nbsp; &nbsp; h = (h < 10) ? "0" + h : h;&nbsp; &nbsp; &nbsp; &nbsp; m = (m < 10) ? "0" + m : m;&nbsp; &nbsp; &nbsp; &nbsp; s = (s < 10) ? "0" + s : s;&nbsp; &nbsp; &nbsp; &nbsp; time = h + ":" + m + ":" + s;&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("Clock").textContent = time;&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(ItsShowTime, 1000);}function CurrentTime(){&nbsp; document.getElementById("txt").innerText = time;}ItsShowTime();<!DOCTYPE html><html dir="ltr">&nbsp; <head>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <title>Digital Clock</title>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <input type="button" value="Start rit" onclick="CurrentTime()">&nbsp; &nbsp; <div id="txt"></div>&nbsp; &nbsp; <div id="Clock">&nbsp; &nbsp; </div>&nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript