We start by creating a button and add the title "Start Loop". This title indicates the start state of this button.
We need to use the Repeat X times action for our loop. The Repeat action will execute the flow of actions attached to it the given amount of times. So if you put a value of 5 in the repeat action it will do the flow of actions 5 times.
Add the action and give it a value of 2147483647, which is the highest possible value and which will be more enough to be used as endless. If each iteration takes 25ms it will take 2 years or so to reach the end.
So in an endless loop we want to do some actions of course. In our example we are going to play a beep sound. Add a Play Audio action and point it to a sound you have.
So if we run this it will play the beep sound endlessly in one go so do not start it yet! Touch Portal does not wait for the sound to be done with playing. So this will sound like a machine gun at the moment. Time to fix this.
Our beep sounds duration is 1 second but we also want to silence after it so we will add a Wait for Timer action that will wait for 2 seconds. If you would run this it would now play a beep sound each 2 seconds. But it is still unstoppable. Once this starts runnning it will keep on running endlessly.
This is the final step of our endless cancelable loop. So the first thing we need to do when the button is pressed is to Toggle the button state. This way we can turn on the loop when we press it and we can stop it when we press it again.
We will only start the endless timer when the button is turned on when we press it. To do this I have wrapped the whole Repeat action in one If Statement which will only trigger if the button is on. When we press it again and the toggle action will turn it off again.
Within the Repeat function we now have added the escape logic. If the loop is running it will check each iteration if the button state is off. If it is, it will execute the Stop executing actions in current flow and that will stop all actions from executing within this flow. Because we have made the exit logic part of the running flow it will work as expected.
You now have a button which holds an infinite loop playing a beep each 2 seconds that you can cancel running at any given moment you desire.