Home > Software design >  How do i make a countdown loop
How do i make a countdown loop

Time:01-18

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?

CodePudding user response:

const intervalId = setInterval(()=>{
   ... DO STUFF HERE
  
}, 1000) // will happened every 1000 milliseconds 


// To kill the interval loop
clearInterval(intervalId)

CodePudding user response:

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)
  •  Tags:  
  • Related