Below code will iterate from 1 to 10 with a delay of 1 second (1000 ms) between each iteration.
function loopFinish() {
   console.log('All is complete');
}
for(let i = 1; i < 11; i++) {
   setTimeout(()=>{
      console.log("Iteration number: "+i);   
      if(i === 10){ 
         setTimeout(loopFinish, 1000); 
      }
   },i * 1000);
}
Output would be in the console browser:
Iteration number: 1 
Iteration number: 2 
Iteration number: 3 
Iteration number: 4 
Iteration number: 5 
Iteration number: 6 
Iteration number: 7 
Iteration number: 8 
Iteration number: 9 
Iteration number: 10 
All is complete
 
No comments:
Post a Comment