Note: All examples use jQuery.
The main property of ThreadJS that makes this possible is that each code block() is scheduled to run only after the previous one has completed. This can be overridden as you will learn in future lessons but most of the time this is how you will use it. In this example the first block() is told to loopWhile() the cnt variable is greater than -1 and then close. After which the next block() will execute.
Other types of loops are loop() and loopTime(). Take a look at the examples below before moving on to see how those work and could have been used instead the standard loop method.
We assigned a value of 4 to the loop() method. This tells the block to execute it's code 4 times before closing. Note that loops don't have to have a set count. If you pass no value, then it would loop infinitely.
You will notice the loop executes 4 times despite the interval being 1 second and the total loopTime() being 3 seconds. This is because unlike normal setInterval(), block loops that are valid (meaning they aren't supposed to close do to any condition) will execute immediately on the first pass. So in this case it executed at 0,1,2 and 3 seconds before closing.