2014-04-28 45 views
0

我嘗試使用本地存儲值在網頁上保存定時器,導航到另一頁面,然後再次從點擊初始頁面保存的值開始計時器。本地存儲和javascript保存計時器

我有時間工作,但與分鐘和小時掙扎,不知道我做錯了什麼。

我可以輸入保存的分鐘和小時數值到公式中,但由於公式每秒鐘動態更新,它不能正常工作。

對此的任何幫助將非常感激,謝謝。

下面是代碼:

var secondtime = localStorage.getItem("Seconds"); 
var minutetime = localStorage.getItem("Minutes"); 
var hourtime = localStorage.getItem("Minutes"); 
var sec = localStorage.getItem("Seconds"); 

setInterval(function(){ 

document.getElementById("text").innerHTML; 
document.getElementById("seconds").innerHTML=pad(++sec%60); 
document.getElementById("minutes").innerHTML=pad(parseInt(sec/60,10)); 
document.getElementById("hours").innerHTML=pad(parseInt(sec/6000,10)); 
}, 1000); 

function SaveTime() { 
    localStorage.setItem("Seconds", $("#seconds").text()) 
    localStorage.setItem("Minutes", $("#minutes").text()) 
    localStorage.setItem("Hours", $("#hours").text()) 
} 

$(document).ready(function(e) { 
$("#seconds").text(secondtime); 
$("#minutes").text(minutetime); 
$("hours").text(hourtime); 

<div class="Bottom_Bar"> 

<p class="c1" id="seconds_text"><span id="seconds">00</span></p> 

<p class="c1" id="minutes_text"><span id="minutes">00</span></p> 

<p class="c1" id="hours_text"><span id="hours">00</span></p> 

<div class="Button"> 
<a href="NextPage.html"> onclick="SaveTime()</div> 
+0

重新仔細閱讀:var hourtime = localStorage.getItem(「Minutes」); ;-) – GameAlchemist

回答

1

我想提請你首先要注意的事情是,你正在試圖分鐘再予分配給hourTime變量在您在初始化

var hourtime = localStorage.getItem("Minutes"); 
太看看

並且在下面的簡單的代碼,其存儲並從本地存儲檢索時間

<a href="#" onclick="SaveTime()">Save Current Time</a> | 
<a href="#" onclick="retrieveTime()">Retrieve Saved Time</a> 

<div id="result"></div> 

function SaveTime(){ 
    var date = new Date(); 
    var timeObj = { sec:date.getSeconds(), min:date.getMinutes(), hr:date.getHours() }; 
    localStorage.setItem("timeObj", JSON.stringify(timeObj)); 
    $('#result').append(JSON.stringify(timeObj)+' -- > Saved<br />'); 
} 

function retrieveTime(){ 
    var timeObj = JSON.parse(localStorage.getItem("timeObj")); 
    //You have the time with you now 
    $('#result').append(timeObj.hr+':'+timeObj.min+':'+timeObj.sec+' --> Retrieved<br />'); 
} 

Here is JS Fiddle Demo