JavaScript 倒计时在到达 00:00 时重定向另一个页面

Sci*_*eek 1 javascript php

我有一个 JavaScript 倒计时器设置为 30 分钟。我试图在计时器到达 00:00 时重定向现有页面以重定向到 exit.php 页面

我的代码如下:

function startTimer(duration, display) {
  var timer = duration,
    minutes, seconds;
  setInterval(function() {
    minutes = parseInt(timer / 60, 10)
    seconds = parseInt(timer % 60, 10);

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds;

    if (--timer < 0) {
      timer = duration;
    }
  }, 1000);
}

window.onload = function() {
  var fiveMinutes = 60 * 30,
    display = document.querySelector('#time');
  startTimer(fiveMinutes, display);
};
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="timer">
  <div>Section</div>
  <div class="time">
    <strong>Time left: <span id="time">30:00</span></strong>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏..

She*_* S. 5

这是我的做法,希望这是您想要的:

https://jsfiddle.net/jom9s562/

var time = 30 * 60;
setInterval(function() {
  var seconds = time % 60;
  var minutes = (time - seconds) / 60;
  if (seconds.toString().length == 1) {
    seconds = "0" + seconds;
  }
  if (minutes.toString().length == 1) {
    minutes = "0" + minutes;
  }
  document.getElementById("time").innerHTML = minutes + ":" + seconds;
  time--;
  if (time == 0) {
    window.location.href = "exit.php";
  }
}, 1000);
Run Code Online (Sandbox Code Playgroud)
<div class="timer" onload="timer(1800)">
  <div>Section</div>
  <div class="time">
    <strong>Time left: <span id="time">Loading...</span></strong>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)