本地存储在使用js和html5的游戏中无法正常工作

时间:2013-11-30 02:48:36

标签: javascript html5 save local-storage

<!DOCTYPE>
  <html>
   <script>
    var cash = 1;
    function start(){
         setInterval('add()',1000)
      }


 function add(){
   cash = cash + 1;
   localStorage.setItem('cash',cash)
  document.getElementById('cash').innerHTML = cash;

  }

  function save(){
   localStorage.setItem('cash',cash);

   }

  function load(){
  cash = localStorage.getItem('cash');

  }
  </script>

 <body onLoad="start()">
<p id="lvl">lvl</p>
<p id="cash">cash</p>
<button type="button" onClick="add()">clickMe</button>
<button type="button" onClick="save()">Save</button>
<button type="button" onClick="load()">load</button>
 </body>
  </html>

当我点击保存然后加载它总是看起来像4111和1继续只是在jsfiddle.net上测试它,因为我无法解释它。但我想要它做的是在保存时存储用户当前的现金,然后在点击加载按钮时带回用户之前的现金并继续计算。

1 个答案:

答案 0 :(得分:0)

你能试试吗,

 cash = parseInt(cash,10) + 1;

代替,

cash = cash + 1;

价:

window.localStorage.setItem("key", "value");

参考:http://www.mosync.com/files/imports/doxygen/latest/html5/localstorage.md.html