从当前时间开始经过30秒后,显示30秒过去的通知

时间:2019-10-13 13:46:50

标签: javascript date datetime time set

我需要制作一个带有时间的页面,并每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 lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<section id="main"></section>
	<section id="clock"></section>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

下次再试...

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);