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

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

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 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!