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

191
Views
How can setTimeout run in order

I have two for loop, inside them I have setTimeout function like following code:

for(let i=0;i<3;i++){
    setTimeout(()=>{console.log(i)}, 1000)
}
for(let i=0;i<3;i++){
    setTimeout(()=>{console.log(i)}, 1000)
}

i want the second loop does not executed until after the first loop finished,

I want this result:
0
1
2
0
1
2
- and between 2 numbers wait 1 second.

how i can do that?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I can't comment yet or I would ask clarifying questions so I'll give you what I think you're asking for. If you want a 1 second delay between each number being logged to the console then this will work:

const func = async () => {
  for (let i = 0; i < 3; i++) {
    await new Promise((resolve) =>
      setTimeout(() => {
        console.log(i);
        resolve();
      }, 1000)
    );
  }
  for (let i = 0; i < 3; i++) {
    await new Promise((resolve) =>
      setTimeout(() => {
        console.log(i);
        resolve();
      }, 1000)
    );
  }
};

func();

A quick rundown of what's happening here. I created an outer function named func which I made asynchronous so that I can use the await keyword within it. I then put the setTimeout call inside a new instance of Promise. The promise combined with the await means that javascript will essentially stop at that line until the Promise calls resolve. Once resolve is called that instance of Promise is finished and the await statement stops "blocking" javascript and the callbackque continues. TLDR:

  1. To use await you must be in an asynchronous function.
  2. If await is used in front of a Promise everything will stop until the promise resolves.
  3. Placing the resolve of the promise inside the callback given to setTimeout ensures that we will wait until each timeout finishes BEFORE the next timeout begins.
about 4 years ago · Santiago Trujillo Report

0

I think that you're trying to do sort of a counter

Try something like this:

for(let i=1;i<=3;i++){
   setTimeout(()=>{ console.log(i)}, i*1000 )
}

Let me know if this solve your problem

about 4 years ago · Santiago Trujillo Report

0

This will work

nxt (0, 0);//seed the stack
function nxt(num, itertn){
    if(num == 3){//want till 2
        if(itertn == 0){// first or 2nd iteration
            num =0;
            itertn++;
        }else{
            return;//after 2nd stop
        }
    }
    console.log(num);//the work
    num++;    
    setTimeout(nxt, 1000, num, itertn);//next after a second
}

But there are other ways to do this

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