倒数一次结束后重新导向首页

时间:2019-07-10 10:21:29

标签: javascript countdown countdowntimer

我正在用Java编写一个倒数计时器,以在访客结束时将其重定向到另一个页面。目前,我有一个工作版本的倒计时计时器和重定向,但是,重定向功能现在每秒触发一次,并且页面不断刷新。

如何使功能homepage();countdownPage();仅运行一次?

谢谢

$(document).ready(function() {
  var countDownDate = new Date("July 11, 2019 09:00:00 GMT").getTime();


  function homepage() {
    location.replace("index.html");
  }

  function countdownPage() {
    location.replace("countdown.html");
  }

  var x = setInterval(function() {
    var now = new Date().getTime();

    var distance = countDownDate - now;

    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor(
      (distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
    );
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    document.getElementById("countdown").innerHTML =
      days + "d " + hours + "h " + minutes + "m " + seconds + "s ";

    if (distance < 0) {
      clearInterval(x);
      document.getElementById("countdown").innerHTML = "Please refresh page";
      homepage();
    } else {
      countdownPage();
    }
  }, 1000);
});

0 个答案:

没有答案