我的JavaScript倒计时不会更新。为什么?

时间:2017-05-21 16:23:41

标签: javascript css countdown

我正在尝试根据此示例在我的网站上倒计时:https://codepen.io/SitePoint/pen/MwNPVq

这是我页面上的代码:

var timestamp_today = Math.floor(Date.now() /1000);
var timestamp_event = 1511679600;
var diff = timestamp_event-timestamp_today;
var deadline = new Date(Date.parse(new Date()) + diff*1000);
initializeClock('countdown', deadline);

HTML

<div id="countdown">
    <div>
        <span class="days"></span>
         <div class="smalltext">Jours</div>
    </div>
    <div>
        <span class="hours"></span>
        <div class="smalltext">Heures</div>
    </div>
    <div>
        <span class="minutes"></span>
        <div class="smalltext">Minutes</div>
    </div>
    <div>
        <span class="seconds"></span>
        <div class="smalltext">Secondes</div>
    </div>
</div>

问题是我的div是空白的:没有任何数字。我做错了什么?

1 个答案:

答案 0 :(得分:0)

您必须在<script>关闭之前放置</body>

<body>
  <nav class="midnight">...
  ...</section>
  <script>
    function getTimeRemain....
    ...ializeClock('countdown', deadline);
  <script>
</body>

但如果您的<script>位于头部的外部文件中,则必须采用下一种方式

window.onload=function(){
  function getTimeRemain....
  ...ializeClock('countdown', deadline);
}