简体   繁体   English

localStorage 在 glitch.com 上无法正常工作?

[英]localStorage not working properly on glitch.com?

I created an incremental game, and was interested in storing the variable in localStorage.我创建了一个增量游戏,并有兴趣将变量存储在 localStorage 中。 I decided it would be smart to try it on a small scale first.我决定先在小范围内尝试它是明智的。 I read my fair share of w3 tutorials and stack overflow questions, but I couldn't get it to work.我阅读了相当多的 w3 教程和堆栈溢出问题,但我无法让它发挥作用。 I wrote the following:我写了以下内容:

var val; //declares test variable
function incr() {
  val += 1;
  document.getElementById("count").innerHTML = val; //update the div where the variable is displayed
  console.log(val)
} //called on by an html button
function save() {
  var saveGame = {
    val: val
  }
  localStorage.setItem("saveGame",JSON.stringify(saveGame));
}
setInterval(function() {
  save();
  console.log("Game Saved.")
}, 100)
function saveDataTest() {
  if (localStorage.getItem("saveGame") || null) {
    load();
  } //called on by 'onload' event

}
function load() {
  var loadGame = JSON.parse(localStorage.getItem("saveGame"));
  val = loadGame.val;
} //split string into an object/variable and use said object/variable to define variable
 if (val == undefined){val = 0;}

As I mentioned in the title, I am using Glitch.com and considering I can't find anything wrong on my end I thought it might be the website's fault.正如我在标题中提到的,我正在使用 Glitch.com 并且考虑到我找不到任何问题,我认为这可能是网站的错。 Please, correct me if I made a mistake in my code.如果我在代码中犯了错误,请纠正我。

var val = 0; //declares test variable
function incr() {
  val += 1;
  document.getElementById("count").innerHTML = val; //update the div where the variable is displayed
  console.log(val)
} //called on by an html button
function save() {
  var saveGame = {
    val: val.toString(),
  }
  localStorage.setItem("saveGame",JSON.stringify(saveGame));
}
window.onload = function() {
  loadGame = JSON.parse(localStorage.getItem("saveGame"));
  load();
};
setTimeout(function() {setInterval(function() {
  save();
  console.log("Game Saved.")
}, 10)},400) //use delay so that game does not save immediately after loading

}
function load() {
if (!(localStorage.getItem("saveGame") == null)) {
  val = Number(loadGame.val);}
} //split string into an object/variable and use said object/variable to define variable

Use Number() and.toString() to convert from string to number and vice versa.使用 Number() 和 .toString() 将字符串转换为数字,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 glitch.com 记录输入? - How do I log inputs using glitch.com? 我在 glitch.com 网站上设置了一个 discord bot,但它实际上没有找到 discord.js - I have set a discord bot on glitch.com website but it's actually not finding discord.js 如何在每次打开时停止节点安装? Glitch.com - How can i stop node installing on every opening? Glitch.com angularjs localstorage无法正常工作? - angularjs localstorage not working properly? "localStorage 无法正常工作\/localStorage 覆盖自身" - localStorage not working properly/localStorage overwriting itself Electron:Windows 上的 localStorage 无法正常工作 - Electron: localStorage on Windows is not working properly localStorage.clear 在 Wp 中无法正常工作 - localStorage.clear not working properly in Wp 推送 function 无法正常工作 localStorage Javascript vanilla - Push function is not working properly localStorage Javascript vanilla 数组数据存储到localStorage在Internet Explorer中不起作用,但在chrome中可以正常工作 - array data store to localStorage is not working in internet explorer, but work properly in chrome 单选按钮与文本框绑定到本地存储或cookie不能正常工作 - Radio button bind with textbox to localstorage or cookie not working properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM