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

163
Visualizações
setInterval inside for loops

I'm tyring to put setInterval inside for loops but it's not working. monArray and towers are arrays of objects. Detection() is collision detection function. I want to set the setInterval time as one of the towers values. What can I do?

function Attack()
    {
    for(t = 0; t < towers.length; t++)
        {
        for(m = 0; m < monArray.length; m++)
            {
            if(Detection(towers[t], monArray[m]))
                {
                console.log("attack")
        
                monArray[m].hp -= towers[t].atk //I want to setInterval this part
        
                if(monArray[m].hp <= 0)
                    {
                    clearInterval(int)
                    }
                break;
                }
            }
        }
    }

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

here is my attempt, this should work

let detect = (t, m) => { return Detection(towers[t], monArray[m]);}
let attack = (t, m) => { monArray[m].hp -= towers[t].atk; };
let islessthanorzero = (m) => { return monArray[m].hp <= 0; };
let callAttack = null;

function Attack() {
    callAttack = setInterval(()=>{
    attackLoop:
    for(let t = 0; t < towers.length; t++){
      for(let m = 0; m < monArray.length; m++) {
        if(detect(t, m)){
          attack(t, m);
          if(islessthanorzero(m)){
              if(callAttack != null) clearInterval(callAttack);
            break attackLoop;
          }
        }
      }
    }
  }, 1000); // called every 1000 milliseconds,  1000ms = 1 second
}
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