+ 2

How to pause execution of 'for' loop in javascript

I have nested for loop that contains some code inside include print statement, when use setTimeout() the all code wait for finishing of for and then printed in one time. I want to pause inner 'for' for 1 sec each time counter changed after its code executed and print it's output.

22nd Jun 2018, 7:01 AM
Am²ar Yas²er
Am²ar Yas²er - avatar
1 Réponse
+ 4
Sorry if I get you wrong. So you're having a nested for loop, which involved a constantly changing counter. And you want to print the output only when it is done with it's operation. From what I know, there's no way in JS to actually pause a for loop, and it is also not recommended to use setTimeOut() as a workaround to 'pause' execution. I would suggest you declare a new var for checking condition (var isDone;). At the end of each iteration, isDone set to true if condition is met. Print your output based on isDone. Check out this thread https://stackoverflow.com/questions/16623852/how-to-pause-javascript-code-execution-for-2-seconds
22nd Jun 2018, 8:01 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar