我很难解决这个问题。我制作了一个小型 Javascript 程序,用户在其中输入一个数字并显示一个百分比。它还显示输入数字的总和。我试图在每次用户查看页面时显示该总数。我想使用 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.";
}