为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

料青山看我应如是 2021-11-18 20:30:22
我需要制作一个页面,随着时间的推移,每 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>
查看完整描述

1 回答

?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

下次试试...


let start = (new Date()).getTime();


function showClock(){

    var d = new Date();

    var hours = d.getHours();

    var minutes = d.getMinutes();

    var seconds=d.getSeconds();

    console.log(`${hours}:${minutes}:${seconds}`);

    if(d.getTime() - start >= 1000 * 30) {

      console.log('30 sec');

        start = d.getTime();

    }

}


setInterval(showClock, 5000);


查看完整回答
反对 回复 2021-11-18
  • 1 回答
  • 0 关注
  • 119 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信