Javascript倒计时问题

这是我的第一个线程,所以我不确定我写在正确的部分等,但我有一个非常简单的问题,迫使我写在这里寻求帮助。我已经编写了一些代码来创建倒计时,显示到特定日期还剩多少时间。然而有些东西不起作用。我将添加我的 html 和 JS 代码,如果有人能找到缺陷,我将不胜感激。


function countdown() {

  var now = new Date();

  var eventDate = new Date(2020, 10, 10);

  var currentTime = now.getTime();

  var eventTime = eventDate.getTime();

  var remTime = eventTime - currentTime;

  var s = Math.floor(remTime / 1000);

  var m = Math.floor(s / 60);

  var h = Math.floor(m / 60);

  var d = Math.floor(h / 24);

  h %= 24;

  m %= 60;

  s %= 60;

  if (h < 10) {

    h = "0" + h;

  }

  if (m < 10) {

    m = "0" + m;

  }

  s = (s < 10) ? "0" + s : s;

  document.getElementById("days").textContent = d;

  document.getElementById("days").innerText = d;


  document.getElementById("hours").textContent = h;

  document.getElementById("minutes").textContent = m;

  document.getElementById("seconds").textContent = s;

  setTimeout(countdown, 1000);

}

<pre>

    <!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <link rel="stylesheet" href="./css/reset.css">

    <link rel="stylesheet" href="./css/font-awesome.min.css">

    <link rel="stylesheet" href="./css/laikmatis.css">

    <link rel="stylesheet" href="./css/main.css">

    <title>Puslapis</title>

</head>


      

慕娘9325324
浏览 111回答 1
1回答

米脂

你忘了调用函数countdown();function countdown() {&nbsp; var now = new Date();&nbsp; var eventDate = new Date(2020, 10, 10);&nbsp; var currentTime = now.getTime();&nbsp; var eventTime = eventDate.getTime();&nbsp; var remTime = eventTime - currentTime;&nbsp; var s = Math.floor(remTime / 1000);&nbsp; var m = Math.floor(s / 60);&nbsp; var h = Math.floor(m / 60);&nbsp; var d = Math.floor(h / 24);&nbsp; h %= 24;&nbsp; m %= 60;&nbsp; s %= 60;&nbsp; if (h < 10) {&nbsp; &nbsp; h = "0" + h;&nbsp; }&nbsp; if (m < 10) {&nbsp; &nbsp; m = "0" + m;&nbsp; }&nbsp; s = (s < 10) ? "0" + s : s;&nbsp; document.getElementById("days").textContent = d;&nbsp; document.getElementById("days").innerText = d;&nbsp; document.getElementById("hours").textContent = h;&nbsp; document.getElementById("minutes").textContent = m;&nbsp; document.getElementById("seconds").textContent = s;&nbsp; setTimeout(countdown, 1000);}countdown();&nbsp; &nbsp; <!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <meta http-equiv="X-UA-Compatible" content="ie=edge">&nbsp; &nbsp; <link rel="stylesheet" href="./css/reset.css">&nbsp; &nbsp; <link rel="stylesheet" href="./css/font-awesome.min.css">&nbsp; &nbsp; <link rel="stylesheet" href="./css/laikmatis.css">&nbsp; &nbsp; <link rel="stylesheet" href="./css/main.css">&nbsp; &nbsp; <title>Puslapis</title></head><body>&nbsp; &nbsp; <section>&nbsp; &nbsp; &nbsp; &nbsp; <main>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h1 class="text1">rgrgrgrgrg</h1>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h2 class="text2">rrgrfgrfgvrfgvrfgv</h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="text1">dvbgtbrtfbrfb</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div id="laikmatis">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td id="days">120</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td id="hours">20</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td id="minutes">20</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td id="seconds">20</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Days</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Hours</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Minutes</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Seconds</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <script src="./JS/laikmatis.js"></script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class="text2">efefefefeeeeeeeeeeeeeeeeefefefe e fef efe fefefef ef ef efe fef ef ef efe ef ef ef ef </p>&nbsp; &nbsp; &nbsp; &nbsp; </main>&nbsp; &nbsp; </section></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript