I have a basic timer counting down right here. Starts at 5, goes to zero, no big deal. If I do not include the window.location into the if statement at the bottom to redirect the page after the timer reaches zero and the interval is cleared, there's two things that happen. If I add "window.location" before the clear interval statement, the interval will freeze at four seconds and stop counting down. If I add the window.location after the clear interval, the interval goes down to 4 and then automatically redirects before getting to zero. It's almost as if the code recognizes a "0" in between the 5 and 4 while counting down. Any advice?
<p> You will be redirected in <span id="countdowntimer">5 </span> Seconds</p>
<script type="text/javascript">
var timeleft = 5;
var downloadTimer = setInterval(function() {
timeleft--;
document.getElementById("countdowntimer").textContent = timeleft;
if (timeleft <= 0)
window.location.href = "testpage.html";
clearInterval(downloadTimer);
}, 1000);
</script>