我正在做一个浏览器游戏,但是无论我多么努力,都遇到了无法解决的问题。
单击绿色方块增加计数器
控制台显示变量已保存到localStorage
但在返回网站后,它返回到0
以下是我的整个JS文件。唯一重要的部分在底部,您可以在https://sleepyshepherd.com亲自查看
//Saved Variables
let ashCount = 0;
let firetruck = 0;
//Initialize Game
window.onload = function() {
const realmTab = document.createElement("div");
const settingsTab = document.createElement("div");
const shopTab = document.createElement("div");
const statsTab = document.createElement("div");
const upgradesTab = document.createElement("div");
const burningBtn = document.createElement("div");
const enslavedTab = document.createElement("div");
const e1 = document.createElement("div");
realmTab.id = "realm";
upgradesTab.id = "upgrades";
settingsTab.id = "settings";
shopTab.id = "shop";
statsTab.id = "stats";
enslavedTab.id = "enslaved";
burningBtn.id = "burning";
e1.id = "e1";
const t1 = document.createTextNode("Realm");
realmTab.appendChild(t1);
const t2 = document.createTextNode("Upgrades");
upgradesTab.appendChild(t2);
const t3 = document.createTextNode("Shop");
shopTab.appendChild(t3);
const t4 = document.createTextNode("Stats");
statsTab.appendChild(t4);
const t5 = document.createTextNode("Settings");
settingsTab.appendChild(t5);
document.getElementById("menu").appendChild(realmTab);
document.getElementById("menu").appendChild(upgradesTab);
document.getElementById("menu").appendChild(shopTab);
document.getElementById("menu").appendChild(statsTab);
document.getElementById("menu").appendChild(settingsTab);
document.getElementById("destroy").appendChild(burningBtn);
document.getElementById("destroy").appendChild(enslavedTab);
document.getElementById("enslaved").appendChild(e1);
//Menu Buttons
const viewport1 = document.querySelector(".viewport");
//Realm View Select
document.querySelector("#realm").addEventListener("click", function() {
viewport1.id = "realmView";
});
//Upgrades View Select
document.querySelector("#upgrades").addEventListener("click", function() {
viewport1.id = "upgradeView";
});
//shop View Select
document.querySelector("#shop").addEventListener("click", function() {
viewport1.id = "shopView";
});
//stats View Select
document.querySelector("#stats").addEventListener("click", function() {
viewport1.id = "statsView";
});
//settings View Select
document.querySelector("#settings").addEventListener("click", function() {
viewport1.id = "settingsView";
});
// Destroy
function increaseAsh() {
ashCount += 1;
}
document.querySelector("#destroy").addEventListener("click", increaseAsh);
//Resources
const r1 = document.createElement("div");
r1.id = "r1";
document.querySelector("#resources").appendChild(r1);
const t6 = document.createElement("p");
r1.appendChild(t6);
window.setInterval(function() {
t6.textContent = ashCount;
}, 16);
//load game
let sg = JSON.parse(localStorage.getItem("save"));
sg.ashCount = ashCount;
}
// Save Game Interval
window.setInterval(function() {
let save = {
ashCount: ashCount,
firetruck: firetruck
}
localStorage.setItem("save", JSON.stringify(save));
}, 10000);
最佳答案
您想要sg.ashCount = ashCount;
时写了ashCount = sg.ashCount;
添加一个console.log(sg);
关于javascript - 本地存储在回访时未加载?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60382858/