我無法纏繞我的頭解決此問題。我製作了一個小型Javascript程序,用戶輸入一個數字並顯示百分比。它還顯示輸入的總數。每次用戶查看頁面時,我都會嘗試顯示該總數。我希望使用localStorage來保存號碼並在用戶每次訪問該頁面時將其顯示在屏幕上。如何保存一個數字的localStorage每個用戶輸入一個新號碼的時間增量?
這裏是的jsfiddle演示。問題是我無法弄清楚如何增加一個保存的數字。每次單擊計算按鈕時,我都要添加分鐘數。
這是到目前爲止我的代碼:
var decent = 1200;
var master = 600000;
var minutes;
var decentOriginal;
var masterOriginal;
var totalDecent=0;
var totalMaster=0;
var decentLeft= 1200;
var masterLeft= 600000;
var totalMin = 0;
function compute() {
minutes = document.getElementById('userInput').value;
decentOriginal = minutes/decent * 100;
masterOriginal = minutes/master * 100;
updatePercent();
localStorage.time = localStorage.num + minutes;
document.getElementById('storage').innerHTML = localStorage.time + " minutes";
}
function updatePercent() {
var decentPercent = document.getElementById('decent-percent');
var masterPercent = document.getElementById('master-percent');
var decentCompute = Math.round(decentOriginal*100)/100;
var masterCompute = Math.round(masterOriginal*100)/100;
decentLeft = decentLeft - minutes;
masterLeft = masterLeft - minutes;
totalDecent = totalDecent + decentCompute;
totalMaster = totalMaster + masterCompute;
totalMin = Math.round(totalMin + parseInt(minutes)/60);
if(decentLeft<=0) {
totalDecent = 100;
decentLeft = 0;
decentCompute = 0;
}
if(masterLeft<=0) {
totalMaster = 100;
masterLeft = 0;
masterCompute = 0;
}
if(totalMin>=600000) {
totalMin=600000;
}
decentPercent.innerHTML = totalDecent.toFixed(1) + "%" + " " + decentLeft + " minute(s) to go.";
masterPercent.innerHTML = totalMaster.toFixed(2) + "%" " " + masterLeft + " minute(s) to go.";
document.getElementById('total-min').innerHTML = totalMin+" hours spent.";
}
你怎麼看這問題?它在做什麼錯誤,Javascript控制檯中是否有錯誤?你能演一個小提琴嗎? – Barmar
http://jsfiddle.net/uyr7Z/ – TGarr
什麼是'localStorage.num'?不應該是'localStorage.time'嗎? – Barmar