admin管理员组

文章数量:1405321

I have a JavaScript Countdown timer set for 30 min. I'm trying to redirect the existing page to redirect to the exit.php page when the timer hits 00:00

My code goes as:

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);
};
<script src=".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>

I have a JavaScript Countdown timer set for 30 min. I'm trying to redirect the existing page to redirect to the exit.php page when the timer hits 00:00

My code goes as:

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);
};
<script src="https://ajax.googleapis./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>

Any Help Is Appreciated..

Share Improve this question edited Jun 9, 2018 at 6:08 Grokify 16.4k8 gold badges69 silver badges93 bronze badges asked Jun 9, 2018 at 5:11 Science GeekScience Geek 711 silver badge5 bronze badges 2
  • why you want to use plex algorithm .--> this is a simple way <script>setTimeout(function(){window.location.href='form2.html'},5000);</script> – pedram shabani Commented Jun 9, 2018 at 5:28
  • @pedramshabani because he wants the timer number to change as well – Sheshank S. Commented Jun 9, 2018 at 5:30
Add a ment  | 

1 Answer 1

Reset to default 5

Here's my go at this, hope this is what you wanted:

https://jsfiddle/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);
<div class="timer" onload="timer(1800)">
  <div>Section</div>
  <div class="time">
    <strong>Time left: <span id="time">Loading...</span></strong>
  </div>
</div>

本文标签: phpJavaScript Countdown to Redirect another page when it hits 0000Stack Overflow