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

223
Views
Trying to run a loop for exactly one second, but the loop runs infinitely. why?

i am changing the value of 'aa' after 1 second, but the loop continues to execute.

let aa = true;

setTimeout(function () {
  aa = false;
}, 1000);

for (; aa; ) {
  console.log('aaa');
}
about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

setTimeout is asynchronous, so it will execute only after the current code execution of for loop completed. In your case it will go infinite.

Once for loop started execution, it will stay in the call stack until finished. Since you didn't give any condition there it won't stop

about 4 years ago · Santiago Gelvez Report

0

Your approach doesn’t work for reasons explained by others. You can use something like this instead:

var started = Date.now();
while(Date.now() - started < 1000) {
  console.log("aaa");
};

about 4 years ago · Santiago Gelvez 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!