gpt4 book ai didi

javascript - 使用JS从LocalStorage加载的问题

转载 作者:太空宇宙 更新时间:2023-11-04 15:41:05 25 4
gpt4 key购买 nike

//Set Variables
var cookies = 0;
var cursors = 0;
var prestige = 0;
var cursorCookies = cursors * 0.1;
var save = {
cookies: cookies,
cursors: cursors,
prestige: prestige
}

function loadgame(){
var savegame = JSON.parse(localStorage.getItem("save"));
if (typeof savegame.cookies !== "undefined") cookies = savegame.cookies;
if (typeof savegame.cursors !== "undefined") cursors = savegame.cursors;
if (typeof savegame.prestige !== "undefined") prestige = savegame.prestige;
}
onload(loadgame());

function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}

function CookieClick(number)
{
cookies = cookies + number;
document.getElementById("cookies").innerHTML = round(cookies,2)
}
function buyCursor(){
var cursorCost = Math.floor(10 * Math.pow(1.1,cursors)); //works out the cost of this cursor
if(cookies >= cursorCost){ //checks that the player can afford the cursor
cursors = cursors + 1; //increases number of cursors
cursorCookies = cursors * 0.1;
cookies = cookies - cursorCost; //removes the cookies spent
document.getElementById('cursors').innerHTML = cursors; //updates the number of cursors for the user
document.getElementById('cookies').innerHTML = cookies; //updates the number of cookies for the user
}
var nextCost = Math.floor(10 * Math.pow(1.1,cursors)); //works out the cost of the next cursor
document.getElementById('cursorCost').innerHTML = nextCost; //updates the cursor cost for the user
}




window.setInterval(function(){
CookieClick(cursorCookies);
localStorage.setItem("save",JSON.stringify(save));
document.getElementById('saveinfo').innerHTML = "Saved: " + Date();
}, 1000);

这是我第一次使用 javascript 和 localstorage,所以请耐心等待,谢谢:)

我目前正在尝试的只是加载我正在保存的 3 个变量,但有些事情不太顺利。我的测试站点:http://atz-exportforce320313.codeanyapp.com/

感谢您的帮助!

最佳答案

正如我在 Facebook Messenger 中指出的:

如果您访问 localStorage 中尚未创建的变量,.getItem() 将简单地返回 undefined
如果您通过 JSON.parse() 传输 undefined ,它也只会返回 null
要解决此问题,您只需在尝试使用 JSON.parse() 之前检查它的响应即可。

一个简单的例子:

var savegame = JSON.parse(localStorage.getItem('save'));
if (savegame === null) {
// initialize default values here or simply abort further execution
return;

// if cookies, cursors and prestige are global variables you can access them through window:
var variables = ['cookies', 'cursors', 'prestige'];

variables.forEach(function(name) {
if (name in savegame) window[name] = savegame[name];
});
}

现在您还尝试通过此行保存状态:
localStorage.setItem("保存",JSON.stringify(save));
这将不起作用,因为您没有将变量写入 save
您基本上必须首先将它们从全局变量添加到一起,例如:

var variables = ['cookies', 'cursors', 'prestige'];
variables.forEach(function(name) {
save[name] = window[name];
});
localStorage.setItem('save', JSON.stringify(save));

PS:
一般来说,我不建议在全局范围内定义你的东西

关于javascript - 使用JS从LocalStorage加载的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43865558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com