我需要制作一个带有时间的页面,并每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>
答案 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);