2015-07-02 63 views
0

我在新的JavaScript,我想創建一個的localStorage倒數計時器,從給定的時間開始和結束00:00:00,但它不工作, 當我跑我的代碼顯示值「1506」。倒數計時器沒有在JavaScript顯示

這裏是我的代碼

<script type="text/javascript"> 
      if (localStorage.getItem("counter")) { 
       var CurrentTime = localStorage.getItem("counter"); 
      } 
      else { 
       var Hour = 3; 
       var Minute = 25; 
       var Second = 60; 
       var CurrentTime = Hour.toString() + ":" + Minute.toString() + ":" + Second.toString(); 
      } 


      function CountDown() {         
       document.getElementById('lblDuration').innerHTML = CurrentTime; 
       Second--; 
       if (Second == -1) { 
        Second = 59; 
        Minute--; 
       } 
       if (Minute == -1) { 
        Minute = 59; 
        Hour--; 
       } 
       localStorage.setItem("counter", CurrentTime); 
      } 

      var interval = setInterval(function() { CountDown(); }, 1000); 

    </script> 
+0

你從來沒有修改CURRENTTIME在你的函數'COUNTDOWN()'。 –

+0

嗨@Gerald Schneider 感謝您的評論,我不明白,請你解釋一下嗎? –

回答

1

設置CurrentTime = Hour.toString() + ":" + Minute.toString() + ":" + Second.toString();當localStorage的,請你不要設置小時,分鐘和秒的值。所以當執行倒計時功能時,它發現第二個是未定義的,而第二個 - 將第二個轉換爲NaN。 要解決它只是初始化小時,分鐘和第二個變量。 我已經重構你的代碼一點點希望它能幫助:

function CountDown() { 
    var currentTime = getCurrentTime();       
    printCurrentTime(currentTime) 
    currentTime.second--; 
    if (currentTime.second == -1) { 
     currentTime.second = 59; 
     currentTime.minute--; 
    } 
    if (currentTime.minute == -1) { 
     currentTime.minute = 59; 
     currentTime.hour--; 
    } 
    setCurrentTime(currentTime); 
} 

    function setCurrentTime(newCurrentTime){ 
    if(localStorage) localStorage.setItem("counter", JSON.stringify(newCurrentTime)); 
    else setCurrentTime.storage = newCurrentTime; 
    } 
    function getCurrentTime(){ 
    var result = localStorage ? localStorage.getItem("counter") : setCurrentTime.storage; 
    result = result || {hour:3, minute:25, second:60}; 
    if (typeof(result) === "string")result = JSON.parse(result); 
    result.toString = function(){ 
     return result.hour + ":" + result.minute + ":" + result.second; 
    } 
    return result; 
    } 
    function printCurrentTime(currentime){ 
     var domTag = document.getElementById('lblDuration'); 
     if(domTag) domTag.innerHTML = currentime.toString(); 
     else console.log(currentime); 
    } 
setInterval(function() { CountDown(); }, 1000); 
+0

謝謝@borja gomez 我運行你的代碼,但它是顯示輸出「1506」 –

+0

這很奇怪我已經在Firefox和IE瀏覽器上運行它,沒有任何問題。你在哪裏試圖運行它? –

+0

我在Chrome中運行它,但是當您討論關於不同的瀏覽時,它已成功運行,但是在關閉瀏覽器後它不會停止事件? 我想,當我再次運行該代碼,應該從它再次啓動的默認值 請幫 –

2
  1. 需要聲明的變量Hour, Minute, Second, CurrentTime出方if else塊。在這種情況下,它們不在function CountDown()範圍內。
  2. 你是不是以後localStorage.setItem("counter", CurrentTime);

var Hour = 3; 
 
var Minute = 25; 
 
var Second = 60; 
 
var CurrentTime = Hour.toString() + ":" + Minute.toString() + ":" + Second.toString(); 
 

 

 
function CountDown() { 
 
    document.getElementById('lblDuration').innerHTML = CurrentTime; 
 
    Second--; 
 
    if (Second == -1) { 
 
     Second = 59; 
 
     Minute--; 
 
    } 
 
    if (Minute == -1) { 
 
     Minute = 59; 
 
     Hour--; 
 
    } 
 
    CurrentTime = Hour.toString() + ":" + Minute.toString() + ":" + Second.toString(); 
 
} 
 

 
setInterval(function() { 
 
    CountDown(); 
 
}, 1000);
<div id="lblDuration"></div>

+0

嗨@ozil 感謝您的寶貴意見, 你的代碼工作正常,但你刪除了localStorage,我想把它放在頁面刷新上,以防止我的倒數計時器, –