[英]localstorage variable only writes 0 to index.html
嗨,當我運行這段代碼時,“clickamt”的結果是 0 並且它不會增加:
switch (isNaN(incremental) || isNaN(clickamt) || isnull(incremental) || isnull(clickamt) || isnull(curfloat) || isNaN(curfloat)) {
case true:
storage.setItem("clickamt", "0");
clickamt = (parseFloat(storage.getItem("clickamt")).toFixed(1));
storage.setItem("incremental", "1");
incremental = (parseInt(storage.getItem("incremental")));
storage.setItem("curfloat", "0");
curfloat = (parseFloat(storage.getItem("curfloat")).toFixed(1));
break;
case false:
if (clickamt % 1000 === curfloat) {
curfloat = (moneylevels()).toFixed(1);
}
clickamt = ((clickamt + incremental) + curfloat).toFixed(1);
incremental++;
storage.setItem("clickamt", (clickamt));
storage.setItem("incremental", (incremental).toString());
storage.setItem("curfloat", (curfloat).toString());
break;
}
"clickamt" 的 innerHTML 將 0 且僅 0 寫入 "index.html"。 我希望浮點類型的“clickamt”通過使用 int 類型的增量加上浮點類型的 curfloat 來增加。 “curfloat”每 1000 次“clickamt”增加 0.1。 我不認為這是一個解析問題或類似的問題。 當我重新編碼一次它只會 append 浮動它會增加到 clickamt 的末尾。 這讓我覺得某處可能存在解析問題。
我見過的一些輸出是(0, 0.101011,00.010101,1,11,1111111,1111111111 等)
如果 localStorage 中存在必要鍵的值,您可以檢查頁面加載。 如果缺少該值,則計算並保存新值。 您還可以有一個額外的事件,您可以在該事件上覆蓋此鍵的值,但這將是用戶操作。
function computeRandomValue() {
var data = ["test", "test1", "test2", "test3", "test4"];
var index = Math.floor(Math.random() * 10) % data.length;
return data[index];
}
function setToLocalStorage(newVal) {
var lsKey = "_lsTest";
localStorage.setItem(lsKey, newVal);
}
function getFromLocalStorage() {
var lsKey = "_lsTest";
return localStorage.getItem(lsKey);
}
function initializePage() {
var _val = getFromLocalStorage();
if (!(_val && _val.trim().length > 0)) {
_val = computeAndSaveNewValue();
}
printValue(_val, "lblResult");
}
function computeAndSaveNewValue() {
var newVal = computeRandomValue();
setToLocalStorage(newVal);
printValue(newVal);
return newVal;
}
function printValue(value) {
var id = "lblResult";
document.getElementById(id).innerHTML = value;
}
(function() {
window.onload = initializePage;
})()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.