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

166
Views
cambie el número cada segundo pero también haga que el número baje una vez que se haga clic en el botón

Estoy tratando de hacer un juego de clicker, así que cuando compras cosas, tus puntos bajan y también hacen que los puntos suban cada segundo. Mi problema es que cuando compra la actualización, la reduce en 15 puntos, pero cuando la cosa automática aumenta mis puntos solo en uno, vuelve a 15 o más. Aquí está mi código hasta ahora:

 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

Podría simplificar lo que tiene haciendo que add tome un número para agregar a 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

Hay múltiples situaciones:

  • El setInterval se ejecuta para siempre. Si lo llama varias veces, se ejecutará varias veces para siempre;
  • la función AutoThing no cambiaba la i;
  • use lowerCamelCase en nombres de funciones (funciona con cualquier caso, pero no se recomienda);
  • La i se inicializa con 0, y el método "aumentar" no hace nada cuando i === 0. Tal vez sea un error o una característica;

 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!