作为初学者,我正在创建一个倒计时项目,这是我的代码:
const newYears = '1 Jan 2021';
function countdown() {
const newYearsDate = new Date(newYears);
const currentDate = new Date();
const seconds = (newYearsDate - currentDate) / 1000;
console.log(newYearsDate - currentDate);
}
// initial call
countdown();
setInterval(countdown, 1000);
以上是没有出现在我的实时服务器控制台中的 console.log。
这是我的html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Countdown Timer</title>
<link rel="stylesheet" href="style.css"/>
<script scr="script.js" defer></script>
</head>
<body>
<h1>New Years Eve</h1>
<div class="countdown-container">
<div class="countdown-el days-c">
<p class="big-text" id="days">0</p>
<span>days</span>
</div>
<div class="countdown-el hours-c">
<p class="big-text" id="hours">0</p>
<span>hours</span>
</div>
<div class="countdown-el minutes-c">
<p class="big-text" id="minutes">0</p>
<span>minutes</span>
</div>
<div class="countdown-el seconds-c">
<p class="big-text" id="seconds">0</p>
<span>seconds</span>
</div>
</div>
</div>
</body>
</html>
我确信有一个简单的解决方案。