Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
change the number every second but also make the number go down once button clicked

I am trying to make a clicker game, so when you buy things, it brings your points down while also making the points go up every second. My problem is that when you buy the upgrade, it brings it down by 15 points, but when the auto thing brings my points up by only one, it goes back to 15 or higher. Here is my code so far:

var i = 0;
num = document.getElementById('number');

function Add() {
  i++;
  num.innerText = i;
}

function AutoThing() {
  document.getElementById("number").innerHTML = i - 15;
  setInterval(increase, 1000)
}

function increase() {
  if (i > 0) {
    i++;
    num.innerText = i;
  }
}
<center>
  <p id="number">0</p>
  <br>
  <button onclick="Add()">Add 1</button>
  <br>
  <button onclick="AutoThing()">auto clicker 15$</button>
</center>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could simplify what you have by making add take a number to add to i.

var i = 0;

function add(amount) {
    i += amount;
    document.getElementById('number').innerText = i;
}

function autoThing() {
    add(-15);
}

setInterval(() => add(1), 1000)
<!doctype html>
<html>
<body>
<center>
  <p id="number">
  0
  </p>
  <br>
  <button onclick="add(1)">
  Add 1
  </button>
  <br>
  <button onclick="autoThing()">
  auto clicker 15$
  </button>
</center>
</body>
</html>
about 4 years ago · Juan Pablo Isaza Relatório

0

There are multiple situations:

  • The setInterval runs forever. If you call it multiple times, will run multiple times forever;
  • the function AutoThing was not changing the i;
  • use lowerCamelCase on function names (works with any case, but it's not recommended);
  • The i is initialized with 0, and the "increase" method does nothing when i === 0. Maybe it's a bug, or it's a feature;

var i = 0;
num = document.getElementById('number');

function add() {
    i++;
    num.innerText = i;
}

function autoThing() {
    if (i <= 15) {
        return;
    }
    i-=15;
    document.getElementById("number").innerHTML = i;
    
}

function increase() {
    if (i > 0) {
        i++;
        num.innerText = i;
    }
}

setInterval(increase, 1000)
<!dotype html>
<html>
<body>
<center>
  <p id="number">
  0
  </p>
  <br>
  <button onclick="add()">
  Add 1
  </button>
  <br>
  <button onclick="autoThing()">
  auto clicker 15$
  </button>
</center>
</body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda