# Stopping a loop at the press of a button

**URL:** <https://community.thunkable.com/t/stopping-a-loop-at-the-press-of-a-button/726798>\
**Category:** Questions about Thunkable X\
**Created:** [August 7, 2020, 10:40pm UTC](https://community.thunkable.com/t/stopping-a-loop-at-the-press-of-a-button/726798 "2020-08-07T22:40:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![luke.hall\_2116](https://avatars.discourse-cdn.com/v4/letter/l/5f8ce5/32.png) [@luke.hall\_2116](https://community.thunkable.com/u/luke.hall_2116)\
**Post date:** [August 7, 2020, 10:40pm UTC](https://community.thunkable.com/t/stopping-a-loop-at-the-press-of-a-button/726798/1 "2020-08-07T22:40:06Z")

</div>

Hey all,

I am having trouble making this block respond the way I need it to. Any help is appreciated!

This is the overall block. It is the meat and potatoes of a project I am working on.

 ![Screen Shot 2020-08-07 at 4.24.04 PM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/2/220364a5b64c7abc239215eab5d18f811c76daa4.png)

My problem is with this section below. I would like it to stop as soon as the overall loop is called again (at the press of a button). I have tried to use the “break out of loop” block, but I haven’t gotten it to work the way I want to. If you see a way to achieve this, please let me know.  
 ![Screen Shot 2020-08-07 at 4.24.11 PM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/1/4/14598e40b17e69ef101c013eecc566c590138df8.png)

---

<div class="post-metadata">

**Author:** ![eoinparkinson](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/eoinparkinson/32/143769_2.png) [@eoinparkinson](https://community.thunkable.com/u/eoinparkinson)\
**Post date:** [August 7, 2020, 11:58pm UTC](https://community.thunkable.com/t/stopping-a-loop-at-the-press-of-a-button/726798/2 "2020-08-07T23:58:35Z")

</div>

Hey @luke.hall_2116, welcome to the Community 👋

Using the `forever` block and a `variable`, this can be easily achieved.  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/c/c/ccb5ad5b39ee1f10b017cda179d285fecff30c23.png)

## Example

See these blocks as an example. Here I am adding random numbers to a list, and when I want it to stop, I press the button.

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/f/4/f408c03cfc98f98b690ce2d4dfde80042c452e91.png)

I wouldn’t quite understand how to integrate this directly into your app without a project link of some description, though this doesn’t seem too difficult to implement based on the blocks you’ve provided.

If you’ve any bother with this, just drop a reply here 🙂

---

<div class="post-metadata">

**Author:** ![luke.hall\_2116](https://avatars.discourse-cdn.com/v4/letter/l/5f8ce5/32.png) [@luke.hall\_2116](https://community.thunkable.com/u/luke.hall_2116)\
**Post date:** [August 8, 2020, 12:19am UTC](https://community.thunkable.com/t/stopping-a-loop-at-the-press-of-a-button/726798/3 "2020-08-08T00:19:53Z")

</div>

Thank you, I hadn’t thought of that before. This may be an obvious question, but I haven’t used functions or forever loops much:

Would there be a way to stop this after a certain number of repeats?

In this case, it **could** be stopped if the larger event were called again (entire block in first screenshot), but **would definitely** stop after looping 14 times (as i showed in the second screenshot). Clear as mud?

---

<div class="post-metadata">

**Author:** ![eoinparkinson](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/eoinparkinson/32/143769_2.png) [@eoinparkinson](https://community.thunkable.com/u/eoinparkinson)\
**Post date:** [August 8, 2020, 12:26am UTC](https://community.thunkable.com/t/stopping-a-loop-at-the-press-of-a-button/726798/4 "2020-08-08T00:26:43Z")

</div>

> [@luke.hall\_2116](#):
>
> Would there be a way to stop this after a certain number of repeats?

You sure can!

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/8/b/8b75b76d01e26ef962f116e5b3296346027b743b.png)

This will repeat 10 times, unless it is stopped. If I were to start this again by clicking the `Start Button` then it would repeat it another 10 times. Is this what you would like?

> [@luke.hall\_2116](#):
>
> I haven’t used functions or forever loops much:

Functions are great, and actually very easy to use with Thunkable. Check out the [docs on Functions](https://docs.thunkable.com/functions).

---

<div class="post-metadata">

**Author:** ![luke.hall\_2116](https://avatars.discourse-cdn.com/v4/letter/l/5f8ce5/32.png) [@luke.hall\_2116](https://community.thunkable.com/u/luke.hall_2116)\
**Post date:** [August 8, 2020, 8:49pm UTC](https://community.thunkable.com/t/stopping-a-loop-at-the-press-of-a-button/726798/5 "2020-08-08T20:49:36Z")

</div>

Many thanks for the solution. If you’re up for it, another problem has popped up in it’s wake.

This new issue comes as a result of the repeat block continuously going in the background. If events happen fast enough, an old call of the function will register that the variable has changed back, and events will happen from both the new function being called, as well as the old one.

I need the function to completely stop once the variable tells it to. That way, it won’t interfere if the function is called again

---

<div class="post-metadata">

**Author:** ![eoinparkinson](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/eoinparkinson/32/143769_2.png) [@eoinparkinson](https://community.thunkable.com/u/eoinparkinson)\
**Post date:** [August 9, 2020, 8:15pm UTC](https://community.thunkable.com/t/stopping-a-loop-at-the-press-of-a-button/726798/6 "2020-08-09T20:15:19Z")

</div>

Hi @luke.hall_2116,

Breaking a loop should jump to the next line of code, or at least that is the intended idea of a `break`.

Do you mind **sharing some screenshots** just highlighting the misfitting blocks? If you think it’s an issue of the loop going too fast you can add a wait block. Do keep in mind if a block has a `then do` at the end, this usually means the block may take extra time to execute. In this scenario, you should use the `then do` option, unless intended otherwise.

All the best, Eoin.

---

<div class="post-metadata">

**Author:** ![luke.hall\_2116](https://avatars.discourse-cdn.com/v4/letter/l/5f8ce5/32.png) [@luke.hall\_2116](https://community.thunkable.com/u/luke.hall_2116)\
**Post date:** [August 10, 2020, 4:30pm UTC](https://community.thunkable.com/t/stopping-a-loop-at-the-press-of-a-button/726798/7 "2020-08-10T16:30:53Z")

</div>

This issue is directly related to my previous question. It is the need for an edited solution to what was answered on this thread. This is what I ended up with after your solution:

 ![Screen Shot 2020-08-10 at 10.07.44 AM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/9/2/92a176b88d8172b5da24cc869f3b00437a01532e.png)

It worked like a charm, except for some timing.

For the sake of understanding, I’m referring to the calling of function “load\_Bar”, as well as the function itself running. Let’s say it is called once. It checks 14 times if it needs to break the cycle, and each is spaced out by a variable wait, which will decrease as time goes on (to make the game harder). It starts at .5 seconds. If the function is called a second time before the total wait (7 seconds) is up, the new instance of the function will work (because it has just been called) in addition to the old instance of the function (which senses that the variable {break\_Loading\_Bar\_Cycle} has opened the loop again). Therefore, the results will be doubled. This is bad.

I need a solution that as soon as the variable {break\_Loading\_Bar\_Cycle} says so, that instance of the function “load\_Bar”, or included loop, is permanently closed to make room for the new one.

Thanks, Luke

---

<div class="post-metadata">

**Author:** ![eoinparkinson](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/eoinparkinson/32/143769_2.png) [@eoinparkinson](https://community.thunkable.com/u/eoinparkinson)\
**Post date:** [August 10, 2020, 8:29pm UTC](https://community.thunkable.com/t/stopping-a-loop-at-the-press-of-a-button/726798/8 "2020-08-10T20:29:49Z")

</div>

Hi @luke.hall_2116,

Firstly, I do not understand how this could work:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/8/b/8b877e99c4e095f6027a8390c0efdf08e96d6657.png)  
The loop here would do nothing.

* * *

> [@luke.hall\_2116](#):
>
> as soon as the variable {break\_Loading\_Bar\_Cycle} says so, that instance of the function “load\_Bar”, or included loop, is permanently closed to make room for the new one.

Did you try to use the `break loop` [block to break the loop](https://docs.thunkable.com/control#repeat-an-event-with-a-loop)?  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/5/a/5a0e907362d8e2a9c36f8177fa628e7cff92731c.png)

---

<div class="post-metadata">

**Author:** ![luke.hall\_2116](https://avatars.discourse-cdn.com/v4/letter/l/5f8ce5/32.png) [@luke.hall\_2116](https://community.thunkable.com/u/luke.hall_2116)\
**Post date:** [August 10, 2020, 11:15pm UTC](https://community.thunkable.com/t/stopping-a-loop-at-the-press-of-a-button/726798/9 "2020-08-10T23:15:57Z")

</div>

I was messing around with other solutions, and the block got reset. In the “load\_Bar” function, it should repeat if “break\_Loading\_Bar\_Cycle ≠ true”

I have messed around with the break loop, but nothing worked. Thunkable Docs say nothing about it, and I’ve never actually seen it successfully used.

If you have any examples or descriptions, that would be very helpful.

Thanks, Luke

---

<div class="post-metadata">

**Author:** ![eoinparkinson](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/eoinparkinson/32/143769_2.png) [@eoinparkinson](https://community.thunkable.com/u/eoinparkinson)\
**Post date:** [August 11, 2020, 12:37am UTC](https://community.thunkable.com/t/stopping-a-loop-at-the-press-of-a-button/726798/10 "2020-08-11T00:37:25Z")

</div>

Hi @luke.hall_2116,

Could you PM me a project link with information such as which screen this is on so i can take a closer look. Only if this is possible, Thanks.

---

<div class="post-metadata">

**Author:** ![ioannis](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/ioannis/32/146956_2.png) [@ioannis](https://community.thunkable.com/u/ioannis)\
**Post date:** [November 8, 2024, 12:26pm UTC](https://community.thunkable.com/t/stopping-a-loop-at-the-press-of-a-button/726798/11 "2024-11-08T12:26:43Z")

</div>


