2017-06-06 27 views
0

我在我的頁面上有一個計時器10分鐘。當時間超過頁面應該刷新自己並刪除會話值。window.location.reload不起作用

它工作得很好,但突然停止工作..計時器卡在0:01。

看起來像是當location.reload()執行後頁面卡住了。資源管理器的狀態欄不斷顯示加載圖標..一遍又一遍。

我的代碼如下:

<script> 
        var counter = setInterval(timer, 1000); 

        function timer() { 
         count = count - 1; 

         // if time exceed then refresh the page. 
         if (count <= 0) { 
          location.reload(); 
         } 
         else 
         { 
          var timerSpan = document.getElementById("timerCountdown"); 

          if (count <= timerTurnToRed) { 
           //Make Red 
           timerSpan.className = "finalCountdown"; 
          } 

          // Calculate remaining minutes & seconds 
          var remainingMinutes = Math.floor(count/60) 

          // Check if it is negative 
          if (remainingMinutes < 0) 
           location.reload(); 

          var remainingSecs = count - (remainingMinutes * 60); 
          if (remainingSecs < 10) { 
           remainingSecs = "0" + remainingSecs.toString(); 
          } 

          timerSpan.innerHTML = remainingMinutes + ":" + remainingSecs; 
         } 
        } 
       </script> 
+0

哪裏是設置 「計數」 的代碼?重新加載後可能爲0。 –

+0

你沒有提供你的問題的所有代碼... –

+0

只是一個瘋狂的猜測,但也許定時器不斷重新加載。就像它做了'location.reload()'那麼在完成重新加載新頁面和清除舊頁面之前,它會再次觸發'location.reload()',從而導致它停止再次加載並重新開始。並結束。嘗試停止一些檢查的時間間隔,也許改變測試'if(count === 0)' –

回答

0

不要把它在哪裏按預期工作的問題。 StackOverflow代碼評估器將防止重新加載。

var count = 15; 
 
var timerTurnToRed = 10; 
 
var counter = setInterval(timer, 1000); 
 

 
        function timer() { 
 
         count--; 
 

 
         // if time exceed then refresh the page. 
 
         if (count <= 0) { 
 
          location.reload(); 
 
         } 
 
         else 
 
         { 
 
          var timerSpan = document.getElementById("timerCountdown"); 
 

 
          if (count <= timerTurnToRed) { 
 
           //Make Red 
 
           timerSpan.className = "finalCountdown"; 
 
          } 
 
          
 
          var remainingMinutes = Math.floor(count/60); 
 

 
          // Check if it is negative 
 
          if (remainingMinutes < 0) 
 
           location.reload(); 
 

 
          var remainingSecs = count - (remainingMinutes * 60); 
 
          if (remainingSecs < 10) { 
 
           remainingSecs = "0" + remainingSecs.toString(); 
 
          } 
 

 
          timerSpan.innerHTML = remainingMinutes + ":" + remainingSecs; 
 
         } 
 
        }
.finalCountdown {color: red;}
<div id="timerCountdown"></div>

+1

這就是爲什麼我們猜測代碼來設置count '是問題。 –

相關問題