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

227
Visualizações
How can I get setInterval() to increase by 1 instead of 12?

My game has two players that get random numbers, and the person who has the bigger number gets 1 "win". My while loop is for the "auto-roll" button, and instead of clicking "roll dice" each time, auto-roll will do it for you until one player has wins == game limit # (bestof.value). No matter where I put my setInterval it increases by a bunch at a time. If bestof.value = 10 then each interval displays at least 10 wins for one player at a time.

checkBox.checked = input checkmark that enables auto-roll feature. So this setInterval will only be active while the auto-roll loop is active.

Anyways, what am I doing wrong?

button.addEventListener("click", myFunction);
    function myFunction() {
        let random = Math.floor((Math.random() * 6) + 1);
        let random2 = Math.floor((Math.random() * 6) + 1);
        screenID.innerHTML = random;
        screenIDD.innerHTML = random2; 
        if (random > random2){
            winNumber.innerHTML = ++a;
        } else if(random2 > random){
            winNumba1.innerHTML = ++b;
        } else {
            console.log("Draw");
        }
        if (a > b){
            winNumber.style.color = 'white';
            winNumba1.style.color = 'black';
        } else if(b > a){
            winNumba1.style.color = 'white';
            winNumber.style.color = 'black';
        } else {
            winNumber.style.color = 'black';
            winNumba1.style.color = 'black';
        }
        if (checkBox.checked){
            setInterval(myFunction, 2000)
            while(a < bestof.value && b < bestof.value){
            myFunction();
          }};
        if (winNumba1.innerHTML == bestof.value){
            winAlert.style.display = "flex";
            console.log('winNumba1 wins!');
        } else if (winNumber.innterHTML == bestof.value){
            winAlert.style.display = "flex";
            console.log('winNumber wins!');
        } else {}
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I wrote a simplified js only version of your game here since I don't have html at hand, but I am sure you can adjust it to your environment.

Main difference: I check if someone won and use return to stop the function If no one won and autoplay is activated I autoplay after 500ms again.

let playerA = 0
let playerB = 0
let autoPlay = true
let bestOf = 3

function myFunction() {

  let random = Math.floor((Math.random() * 6) + 1);
  let random2 = Math.floor((Math.random() * 6) + 1);
  console.log("New Round " + random + " vs " + random2)
  if (random > random2) {
    playerA++
  } else if (random2 > random) {
    playerB++
  } else {
    console.log("Draw");
  }
  if (playerA > playerB) {
    console.log("a is winning")
  } else if (playerB > playerA) {
    console.log("b is winning")
  } else {
    console.log("There has been a draw")
  }
  if (playerA == bestOf) {
    console.log('A won');
    return
  } else if (playerB == bestOf) {
    console.log('B won');
    return
  }
  if (autoPlay) {
    setTimeout(myFunction, 500)
  };

};
myFunction()

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