dongzen5577 2014-01-25 11:52
浏览 103
已采纳

倒计时器中的JavaScript重定向循环

I have a Javascript countdown timer in my page. Once the counter reachers zero, I want to redirect the user to another page. However, my page is entering a redirect loop and keeps redirecting to itself.

var count=20;

setInterval(timer, 1000);

function timer()
{
    if(count > 0)
    {
        count = count - 1;
    }
    else
        window.location = "<?= site_url('quiz/complete/' . $session->id); ?>";

    document.getElementById("timer").innerHTML = count;
}
  • 写回答

2条回答 默认 最新

  • doulu5717 2014-01-25 11:55
    关注

    You need to remove interval by clearInterval() function. Something like:

    var count=20;
    
    var interval = setInterval(timer, 1000);
    
    function timer()
    {
      if(count > 0)
      {
        count = count - 1;
      }
      else {
        clearInterval(interval);
        window.location = "<?= site_url('quiz/complete/' . $session->id); ?>";
      }
      document.getElementById("timer").innerHTML = count;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?