I need to make a clicker game and in it i need an upgrade that adds clicks per seconds. How do i make a loop that every second it adds to the points variable? this is in JavaScript by the way. I have tried googling an answer and that did not help, an anyone help me with this?
Asked
Active
Viewed 27 times
0
-
Does this answer your question? [Calling a function every 60 seconds](https://stackoverflow.com/questions/3138756/calling-a-function-every-60-seconds) – Frederick Reynolds Jan 19 '22 at 01:31
2 Answers
0
const intervalId = setInterval(()=>{
... DO STUFF HERE
}, 1000) // will happened every 1000 milliseconds
// To kill the interval loop
clearInterval(intervalId)

Aviv Ben Shahar
- 268
- 1
- 8
0
Do you want the clicks per seconds feature? I am not sure what exactly you looking for.
as Aviv Ben Shahar already answered to use setInterval.
here is the code you might looking for.
const clicksdom = document.getElementById("clicks");
const hclicksdom = document.getElementById("hclicks");
var clicks=0;
var hclicks=0;
document.addEventListener("click",()=>{
clicks+=1;
});
const intervalId = setInterval(()=>{
if(clicks>=hclicks){
hclicks = clicks;
}
hclicksdom.innerText = hclicks;
clicksdom.innerText = clicks;
clicks = 0;
}, 1000)
<div>
<p>Clicks Per Seconds</p>
<p>highest cps <span id="hclicks">--</span></p>
<p>current cps <span id="clicks">--</span></p>
</div>
// To kill the interval loop
clearInterval(intervalId)

aekit
- 405
- 4
- 14