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

224
Vistas
How do I set an Interval only onClick in Javascript?

So I am building a Pokemon Idle RPG and I coded in a "buyStarter()" function, so when a player has enough money, they can buy a starter pokemon that will Autoclick for them. For some reason, the interval doesn't begin when the function "buyStarter()" begins.

Below is the with the called function onClick and beneath is the function that is suppose to set interval if successful.

var starterCost = 250;
var starter = 0;
var autoclick = 0;

function buyStarter() {
  if (count >= starterCost && starter == 0) {
    autoclick = 1;
    starter = 1;
    count = count - starterCost;
    document.getElementById("starterCost").innerHTML = starterCost;
    document.getElementById("starter").innerHTML = starter;
    document.getElementById("autoclick").innerHTML = autoclick;
  } else if (starter == 1) {
    alert("you have already purchased a starter pokemon");
  } else {
    alert("You cannot purchase a starter pokemon");
  }
  setInterval(function(buyStarter) {
    findPokeballs(autoclick);
  }, 1000);
}
<input type="image" id="" src="images/pokemon/black-white/1.png" onClick="buyStarter()" value="Send">

The process of the feature is, you click and once you earn enough money you can purchase a pokemon to click for you (idle/autoclicker).

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You mde three mistakes:

  1. You added a parameter to the function getting called by setInterval, but setInterval has no idea what to pass as that parameter
  2. You gotta define count somewhere, so var count; at the top where you're defining other variables
  3. You could just do a setInterval from the start, and instead have a boolean that switches to true when you want the function to start getting called, this gives you the advantage of more customizability later on (like adding different statements that run/don't run in your setInterval.
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