Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!