我有两个JavaScript函数,分别应该 "保存 "和 "加载 "一个变量。保存按钮可以将变量保存到本地存储,但是加载按钮并没有将本地存储信息加载到变量中。它应该是将变量设置为本地存储的一个值。
基本上,我需要给一个变量分配一个值(它存在于loadave函数之外)。这个值需要来自本地存储中的一个值。
function saveVars() {
localStorage.setItem('money', money);
}
function loadVars() {
var money = localStorage.getItem('money');
}
如果有帮助的话,这里是完整的文件。我只想保存钱这个变量。相关函数在底部)。
var moneyAdd = 1;
var money = 1000
var crystals = 0
var wood = 0;
var metal = 0;
var sword = 0;
var rsword = 0;
var hasAnvil = false;
function getCrystal() {
crystal += 1
}
function getMetal() {
metal += 1
}
function getWood() {
wood += 1
}
function displayInventory() {
alert("Here is your inventory:\n" + money + " Money\n" + crystals + " Crystals\n" + wood + " Wood\n" + metal + " Metal\n" + sword + " Sword(s)\nHas Anvil (" + hasAnvil + ")\n" + rsword + " Reinforced Swords\n")
}
function addMoney() {
money += moneyAdd;
}
function displayMoney() {
alert("You have " + money + " moneys!");
}
function displayCrystals() {
alert("You have " + crystals + " crystals!");
}
function displayWood() {
alert("You have " + wood + " wood!");
}
function displayMetal() {
alert("You have " + metal + " metal!");
}
function craftSword() {
var r = confirm("Are you sure you want to buy one basic sword for 2 metal, 1 wood, and 1 crystal?");
if (r == true) {
if (crystals >= 1 && wood >= 1 && metal >= 2) {
wood -= 1;
crystals -= 1;
metal -= 2;
sword += 1;
alert("Transaction completed. You now have " + sword + " basic swords.")
} else {
alert("Insufficient Funds. Transaction Cancelled.");
}
} else {
alert("Transaction cancelled.");
}
}
function craftRSword() {
var r = confirm("Are you sure you want to craft one reinforced sword with 1 basic sword and 2 metal with an anvil?");
if (r == true) {
if (sword >= 1 && metal >= 2 && hasAnvil == true) {
sword -=1;
metal -= 2;
rsword += 1;
alert("Transaction completed. You now have " + rsword + " reinforced swords.")
} else {
alert("Insufficient Funds. Transaction Cancelled.");
}
} else {
alert("Transaction cancelled.");
}
}
function craftAnvil() {
var r = confirm("Are you sure you want to craft one iron anvil for 4 metal?");
if (r == true) {
if (metal >= 4) {
metal -= 4;
hasAnvil = true;
alert("Transaction completed. You now have an anvil (" + hasAnvil + ").")
} else {
alert("Insufficient Funds. Transaction Cancelled.");
}
} else {
alert("Transaction cancelled.");
}
}
function sellSword() {
var r = confirm("Are you sure you want to sell a basic sword and grab 150 money?");
if (r == true) {
if (sword >= 1) {
money += 150;
sword -= 1;
alert("Transaction completed. You now have sold a basic sword and now have " + money + " moneys.")
} else {
alert("Insufficient Funds. Transaction Cancelled.");
}
} else {
alert("Transaction cancelled.");
}
}
function sellRSword() {
var r = confirm("Are you sure you want to sell a reinforced sword and grab 250 money?");
if (r == true) {
if (rsword >= 1) {
money += 250;
rsword -= 1;
alert("Transaction completed. You now have sold a reinforced sword and now have " + money + " moneys.")
} else {
alert("Insufficient Funds. Transaction Cancelled.");
}
} else {
alert("Transaction cancelled.");
}
}
function buyCrystal() {
var r = confirm("Are you sure you want to buy a Crystal for 100 Moneys?");
if (r == true) {
if (money >= 100) {
money -= 100;
crystals += 1;
alert("Transaction completed. You now have " + crystals + " crystals.")
} else {
alert("Insufficient Funds. Transaction Cancelled.");
}
} else {
alert("Transaction cancelled.");
}
}
function buyWood() {
var r = confirm("Are you sure you want to buy one wood for 20 Moneys?");
if (r == true) {
if (money >= 20) {
money -= 20;
wood += 1;
alert("Transaction completed. You now have " + wood + " wood.")
} else {
alert("Insufficient Funds. Transaction Cancelled.");
}
} else {
alert("Transaction cancelled.");
}
}
function buyMetal() {
var r = confirm("Are you sure you want to buy one metal for 40 Moneys?");
if (r == true) {
if (money >= 40) {
money -= 40;
metal += 1;
alert("Transaction completed. You now have " + metal + " metal.")
} else {
alert("Insufficient Funds. Transaction Cancelled.");
}
} else {
alert("Transaction cancelled.");
}
}
function saveVars() {
localStorage.setItem('money', money);
}
function loadVars() {
var money = localStorage.getItem('money');
}
从你的例子中,我可以假设有两种选择。
1) 你想把值 "加载 "到一个变量中。全球性 变量名为 "money"?你的代码不工作,因为你声明了一个名为 "钱 "的 本地 变量 "money",它只存在于函数 loadVars
. 不要声明你的变量(即不要添加关键字--------------)。var
或函数中的任何等价物)。) 赋值 money = …
只有在更特定的作用域中没有其他名为 "money "的变量时,才会分配给全局变量 "money"--即使全局变量没有被声明,它也会这样做。
function loadVars() {
money = localStorage.getItem('money');
}
2) 你想要 loadVars
到 返回 的值,然后你就可以将其分配给全局变量。
function loadVars() {
return localStorage.getItem('money');
}
var money = loadVars();