在JAVASCRIPT中页面重新加载时保存变量的当前值

时间:2017-02-01 14:55:12

标签: javascript

如果我有

,是否有办法在页面刷新时保留变量值

var x = 0;

然后用户做了一些让这个值变为

的东西

X = 4;

然后刷新页面,是否有办法为x变量保留4的值,否则它将始终重置为原始值?

我已经编写了2个函数来设置并得到如下值:

     function SetlifeCounter()
      {
        life = 3;
        localStorage.setItem("life", life);
       }

      function GetlifeCounter()
       {
         life1 = localStorage.getItem("life");
           life1=life1-1;
         return life1;
       }

然后用这种方式打电话给他们:

  var mainLife;
  SetlifeCounter();
    mainLife=GetlifeCounter();
    while(mainLife!=0)
    {

        window.location.reload();
    }

但是,当我运行此页面冻结... 我做错了什么?

P.S这是为​​了跟踪号码。每次你死亡(页面重新加载)游戏中的生命都会被扣除。

2 个答案:

答案 0 :(得分:2)

是的,请使用 enter image description here

使用持久值时,您需要检查是否已保存某个值(来自用户之前的访问),如果是,请使用该值。如果没有,您需要创建一个值并将其设置为存储,以便在后续访问中使用它。

这通常是这样做的:

var x = null;

// Check to see if there is already a value in localStorage
if(localStorage.getItem("x")){

  // Value exists. Get it assign it:
  x = localStorage.getItem("x");

} else {

  // Value doesn't exist. Set it locally and store it for future visits
  x = 3.14;
  x = Math.floor(x);

  localStorage.setItem("x", x); 

}

在您的示例中,您遇到了各种各样的问题,但浏览器锁定是由while循环引起的,当生命计数不为零时,它会创建一个无限循环。您必须非常小心while循环并确保它们始终具有将被命中的终止条件。但是,就你的目的而言,无论如何这都是不必要的。

有关正确使用localStorage的详细信息,请参阅下面的评论。

注意:由于沙盒,此代码在Stack Overflow代码段编辑器中无法使用,但您可以看到此工作示例 localStorage



// This is where you will store the lives remaining
// You don't need several variables to keep track of this
// (mainLife, life, life1). Just modify this one variable as needed.
var lifeCount = null;

// As soon as the user arrives at the page, get or set the life count 
window.addEventListener("DOMContentLoaded", initLifeCount);

// By convention use camelCase for identifier names that aren't constructors
// and put opening curly brace on same line as declaration
function initLifeCount() {

  // You need to test to see if an item exists in localStorage before getting it
  if(localStorage.getItem("life")){
  
    // And, remember that localStorage values are stored as strings, so you must
    // convert them to numbers before doing math with them.
    lifeCount = parseInt(localStorage.getItem("life"), 10) - 1;
    
    // Test to see if there are any lives left
    if(lifeCount === 0){
    	// Invoke "Game Over" code
      alert("Game Over!");
    }
        
  } else {
    // User hasn't previously stored a count, so they must be starting a new game
    lifeCount = 3;
  }
  
  // Any time the count changes, remember to update it in localStorage
  updateLifeCount();

  // Temporary code for debugging:
  console.log("Life count is: " + lifeCount);
}

// Call this function after the life count changes during game play
function updateLifeCount(){
  localStorage.setItem("life", lifeCount)
}




如果您打开开发人员工具(F12)并导航以查看localStorage,则每次重新运行小提琴时,您都可以看到值减少。您还可以在控制台中看到该值。

here

enter image description here

答案 1 :(得分:0)

很多。您可以选择cookie,本地存储,Web sql,远程后端......

请参阅有关在Cookie中设置值的问题:

Set cookie and get cookie with JavaScript

对于本地存储,请使用:

localStorage.setItem("x", x);