我在我的小游戏中添加了一个保存和加载部分,现在当我去btc或dash时,它会转到NAN。
var satoshi = 0;
var hash = 1;
var litoshi = 0;
var lhash = 1;
var updateTimer;
var minebtcTimer;
var dhash = 1;
var dash = 0;
var mineDashTimer;
var updatesTimer;
var hashPerSecond = document.getElementsByClassName("hashPerSecond");
hashPerSecond[0].innerHTML = hashPerSecond[0].innerHTML + " " + hash;
hashPerSecond[1].innerHTML = hashPerSecond[1].innerHTML + " " + dhash;
function save() {
dash = localStorage.getItem("dash");
dash = parseInt(dash);
satoshi = localStorage.getItem("satoshi");
satoshi = parseInt(satoshi);
dhash = localStorage.getItem("dhash");
dhash = parseInt(dhash);
hash = localStorage.getItem("hash");
hash = parseInt(hash);
}
function stops() {
clearTimeout(updatesTimer);
clearTimeout(mineDashTimer);
}
function minebtc() {
satoshi = satoshi + hash
update()
}
function updates() {
document.getElementById('hashers').innerHTML = "Dashosis: " + dash;
updatesTimer = setTimeout(updates, 10000000000)
mineDashTimer = setTimeout(minedash, 1000);
}
function update() {
document.getElementById('hashspersecond').innerHTML = "Satoshis: " + satoshi;
updateTimer = setTimeout(update, 1000000)
minebtcTimer = setTimeout(minebtc, 1000);
}
function minedash() {
dash = dash + dhash
updates()
}
function stop() {
clearTimeout(updateTimer);
clearTimeout(minebtcTimer);
}
function boop() {
localStorage.setItem("satoshi", satoshi);
localStorage.setItem("hash", hash);
localStorage.setItem("dhash", dhash);
localStorage.setItem("dash", dash);
}

.column {
padding: 15px;
float: left;
}
body {
background-image: url('https://i.ytimg.com/vi/h6IlzDxjmR0/maxresdefault.jpg');
background-size: cover;
}
div.div {
background-color: lightgrey;
width: 120px;
border: 12px solid yellow;
padding: 19.5px;
margin: 19.5px;
}

<body onload="save()" onunload="boop()">
<div class="div">
Crystals: 0
</div>
<div class="div">
<center>
<h4 class="hashPerSecond">
Hashs Per Second:
</h4>
</center>
<center>
<p id="hashspersecond">Satoshis: 0</p>
</center>
<button onclick="minebtc()"><img src="kop.png" height=28 width=32>Mine BTC</button>
<button onclick="stop()"><img src="kop.png" height=28 width=32>Stop Mining BTC</button>
</div>
<div class="div">
<center>
<h4 class="hashPerSecond">
Hashs Per Second:
</h4>
</center>
<center>
<p id="hashers">Dashosis: 0 </p>
<button onclick="minedash()"><img src="dashed.png" height=28 width=32>Mine DASH</button>
<button onclick="stops()"><img src="dashed.png" height=28 width=32>Stop Mining DASH</button>
</center>
</div>
</body>
&#13;
如果你帮助我,我们非常感谢!
答案 0 :(得分:0)
我修改了你的代码并从代码中删除了一些东西。
this Stackoverflow link上所述 document.write()
指出document.write()
会导致DOM
被销毁,而在某些浏览器中会导致某些global variables
被删除。< / p>
这里是JSFiddle链接:https://jsfiddle.net/dLtg5pvh/2/