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).
You mde three mistakes:
setInterval, but setInterval has no idea what to pass as that parametercount somewhere, so var count; at the top where you're defining other variablessetInterval 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.