Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!