У меня есть две JavaScript функции, которые должны «сохранить» и «загрузить» переменную соответственно. Кнопка сохранения позволяет сохранить переменную в локальном хранилище, но кнопка загрузки не загружает информацию о локальном хранилище в переменную. Предполагается установить для переменной значение из локального хранилища.
По сути, мне нужно присвоить значение переменной (которая существует вне функций загрузки / сохранения). Это значение должно исходить из значения в локальном хранилище.
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');
}