Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda