Do several things at once - cl4cnam/funcSug GitHub Wiki
In this tutorial, we are going to do several things in the same time.
Prepare your environment
Do as in Hello World tutorial except that you change the names: severalAtOnce, severalAtOnce.html, severalAtOnce.fg.
Copy the files ducks.mp3, birds.mp3, barking.mp3 into the directory severalAtOnce.
Write the program
Write these lines into severalAtOnce.fg:
parallel ||
playSoundFile('ducks.mp3')
||
playSoundFile('birds.mp3')
||
playSoundFile('barking.mp3')
displayNewMessage('The End')
Explanation
The program is made up of two parts:
The first part
parallel ||
playSoundFile('ducks.mp3')
||
playSoundFile('birds.mp3')
||
playSoundFile('barking.mp3')
In this part, all the sounds are played at once. They start at the same time. This first part is called a 'parallel construct'.
[!WARNING] The sounds don't finished at the same time. The 'parallel construct' finishes only when all the sounds are finished :open_mouth:.
[!TIP] Don't worry! You'll see later how to interrupt before all the sounds are finished.
The second part
displayNewMessage('The End')
[!NOTE] The second part is executed only when the first part is finished (as usual) and, remember, the first part is finished when all the sounds are finished. So, the 'The End' message is only displayed when all the sounds are finished.
The whole
So when you load severalAtOnce.html, here is the timeline:
A little further experiment
The birds sound lasts 55 seconds. If you replace playSoundFile('birds.mp3') by waitSeconds(55), you obtain the same logic: waiting 55 seconds is like playing a silent sound that lasts 55 seconds.
Then, here is the timeline:
To sum up
A "parallel construct" has the form:
parallel ||
block1
||
block2
||
block3
[!IMPORTANT] It executes "block1", "block2" and "block3" in parallel (just logical parallelism). That is:
- :+1: "block1", "block2" and "block3" are started in the same time;
- :+1: "block1", "block2" and "block3" are executed in the same time;
- :warning: but "block1", "block2" and "block3" aren't finished in the same time;
- :open_mouth::warning: the "parallel construct" finishes only when all the blocks are finished.