从当前时间起 30 秒后,显示 30 秒已过的通知

我需要制作一个页面,随着时间的推移,每 5 秒更改一次时间,30 秒后我需要显示 30 秒已经过去的通知


function showClock(){

    var d=new Date();

    var hours=d.getHours();

    var minutes=d.getMinutes();

    var seconds=d.getSeconds();

    var clock=document.getElementById("clock");

    clock.innerHTML=`<h1>${hours}:${minutes}:${seconds}</h1>`;

    

   }

  setInterval(showClock,1000);

<!DOCTYPE html>

<html>

<head>

  <meta charset="UTF-8">

  <title>Document</title>

</head>

<body>

  <section id="main"></section>

  <section id="clock"></section>

  </body>

</html>


料青山看我应如是
浏览 120回答 1
1回答

收到一只叮咚

下次试试...let start = (new Date()).getTime();function showClock(){&nbsp; &nbsp; var d = new Date();&nbsp; &nbsp; var hours = d.getHours();&nbsp; &nbsp; var minutes = d.getMinutes();&nbsp; &nbsp; var seconds=d.getSeconds();&nbsp; &nbsp; console.log(`${hours}:${minutes}:${seconds}`);&nbsp; &nbsp; if(d.getTime() - start >= 1000 * 30) {&nbsp; &nbsp; &nbsp; console.log('30 sec');&nbsp; &nbsp; &nbsp; &nbsp; start = d.getTime();&nbsp; &nbsp; }}setInterval(showClock, 5000);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript