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>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>Hay múltiples situaciones:
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>