0

So, I have my btc and hashs, and when I click start mining, I want it to have every 1 second it adds btc to my balance equal to the number of hashs I have, so if I have 1 satoshi, and 1 hash and I spend 1 second mining, I get 2 btc, then , then 4.

Also can you guys check and make sure I put my timers the right way?

My code is:

<html>
<head>
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>


<style>
</style>

</head>


<body>
<pre> </pre>
<div class="container">

  <div class="card-deck">
    <div class="card bg-primary">
      <div class="card-body text-center">
    <i class="fab fa-btc" height="32" width ="32"></i>
    <p class="card-text">Satoshis: <span id="btc"></span></p>

      </div>
    </div>
    <div class="card bg-warning">
      <div class="card-body text-center">
        <p class="card-text">Hashs: <span id="hashs"></span></p>
      </div>
    </div>
    <div class="card bg-success">
      <div class="card-body text-center">
        <p class="card-text">upgrades button here with popup tab</p>
      </div>
    </div>
    <div class="card bg-danger">
      <div class="card-body text-center">
        <p class="card-text"><p id="btcmina"><button type="button" onclick="btcmining()" class="btn btn-light">Begin Mining</button></p></p>
      </div>
    </div>  
  </div>
</div>
<pre> </pre>
<div class="container">

  <div class="card-deck">
    <div class="card bg-primary">
      <div class="card-body text-center">
    <i class="fab fa-monero" height="32" width ="32"></i>
    <p class="card-text">monotoshis here</p>

      </div>
    </div>
    <div class="card bg-warning">
      <div class="card-body text-center">
        <p class="card-text">mono hashs here</p>
      </div>
    </div>
    <div class="card bg-success">
      <div class="card-body text-center">
        <p class="card-text">upgrades button here with popup tab</p>
      </div>
    </div>
    <div class="card bg-danger">
      <div class="card-body text-center">
        <p class="card-text">Start mining button here with css button</p>
      </div>
    </div>  
  </div>
</div>

</body>




<script>
var btc = Number(localStorage.getItem("btc") || "1");
var hashs = Number(localStorage.getItem("hashs") || "1");

function btcmining() {
    document.getElementById('btcmina').innerHTML = "<p id=mini><button type=button class=btn btn-primary onclick=stopbtc()>Stop Mining</button></p>";

    updateTimer = setTimeout(updatey, 1000000)
        minebtcTimer = setTimeout(btcmining, 1000);
}

function update() {
  document.getElementById('btc').innerHTML = btc;
  document.getElementById('hashs').innerHTML = hashs;
  localStorage.setItem("btc", btc);
  localStorage.setItem("hashs", hashs);
}
function updatey() {
  document.getElementById('btc').innerHTML = btc;
  document.getElementById('hashs').innerHTML = hashs;
  localStorage.setItem("btc", btc);
  localStorage.setItem("hashs", hashs);
  btcmining()
}
function stopbtc() {
    document.getElementById('mini').innerHTML = "<p id=btcmina><button  type=button onclick=btcmining() class=btn btn-primary>Begin Mining</button></p>"
    clearTimeout(updateTimer);
    clearTimeout(minebtcTimer);
}
update();

/*document.getElementById('ao').onclick = function () {
    btc++;
  update();
}
document.getElementById('hash').onclick = function () {
    hashs++;
  update();
}*/
// <button id="ao">Add One</button>
</script>



</html>
asdwd wdad
  • 67
  • 4
  • 7

1 Answers1

0

Read the number from local storage, add to it, then store the new number back in local storage.

setInterval(add_to_localstorage, 1000);

function add_to_localstorage() {
    var btc = Number(localStorage.getItem("btc") || "1");
    btc = btc + 1;
    localStorage.setItem("btc", btc);
}

This just adds 1, if you want to add something else you can replace that line with whatever calculations you actually want to do, such as counting the number of hashes and adding that.

Barmar
  • 741,623
  • 53
  • 500
  • 612