[英]local Storage not working properly in game using js and html5
<!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>
When I click save then load it always appears like 4111 and the 1's keep going just test it on jsfiddle.net since I cannot explain it. 当我单击“保存”然后加载时,它总是显示为4111,而1继续运行就可以在jsfiddle.net上对其进行测试,因为我无法解释它。 But what I want it to do is store the users currents cash when saved then when the load button is clicked bring back the user's cash that he/she had before and keep counting up. 但是我想要做的是保存时存储用户的现金,然后单击“加载”按钮,取回用户以前拥有的现金并继续计数。
Can you try this, 你可以试试这个吗
cash = parseInt(cash,10) + 1;
instead of, 代替,
cash = cash + 1;
Ref: 参考:
window.localStorage.setItem("key", "value");
Ref: http://www.mosync.com/files/imports/doxygen/latest/html5/localstorage.md.html 参考: http : //www.mosync.com/files/imports/doxygen/latest/html5/localstorage.md.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.