Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

225
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda