# Executing and exit from a continuous loop with the same button

**URL:** <https://community.thunkable.com/t/executing-and-exit-from-a-continuous-loop-with-the-same-button/2597360>\
**Category:** Questions about Thunkable X\
**Tags:** data-viewer\
**Created:** [September 15, 2023, 11:53am UTC](https://community.thunkable.com/t/executing-and-exit-from-a-continuous-loop-with-the-same-button/2597360 "2023-09-15T11:53:09Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giuseppe\_Geppo\_Catta](https://avatars.discourse-cdn.com/v4/letter/g/2acd7d/32.png) [@Giuseppe\_Geppo\_Catta](https://community.thunkable.com/u/Giuseppe_Geppo_Catta)\
**Post date:** [September 15, 2023, 11:53am UTC](https://community.thunkable.com/t/executing-and-exit-from-a-continuous-loop-with-the-same-button/2597360/1 "2023-09-15T11:53:10Z")

</div>

Hi,  
i would like to start a counter pressing a button.  
So I will place a while loop inside a button click that increment my counter on certain conditions.  
How can I exit that loop pressing the same button. Being in a loop, it will never trigger the button pressing.  
Which is the best practice for something similar?

---

<div class="post-metadata">

**Author:** ![vishruth-ram](https://avatars.discourse-cdn.com/v4/letter/v/82dd89/32.png) [@vishruth-ram](https://community.thunkable.com/u/vishruth-ram)\
**Post date:** [September 15, 2023, 12:17pm UTC](https://community.thunkable.com/t/executing-and-exit-from-a-continuous-loop-with-the-same-button/2597360/2 "2023-09-15T12:17:30Z")

</div>

If I understood the question correctly,you can use this code

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

And snap your conditions in the while loop

---

<div class="post-metadata">

**Author:** ![Giuseppe\_Geppo\_Catta](https://avatars.discourse-cdn.com/v4/letter/g/2acd7d/32.png) [@Giuseppe\_Geppo\_Catta](https://community.thunkable.com/u/Giuseppe_Geppo_Catta)\
**Post date:** [September 15, 2023, 1:06pm UTC](https://community.thunkable.com/t/executing-and-exit-from-a-continuous-loop-with-the-same-button/2597360/3 "2023-09-15T13:06:07Z")

</div>

If you are in the “Repeat while” loop, nobody could change the stop/start condition and the button click will not be triggered no more. So you will never exit the loop

---

<div class="post-metadata">

**Author:** ![Giuseppe\_Geppo\_Catta](https://avatars.discourse-cdn.com/v4/letter/g/2acd7d/32.png) [@Giuseppe\_Geppo\_Catta](https://community.thunkable.com/u/Giuseppe_Geppo_Catta)\
**Post date:** [September 15, 2023, 1:39pm UTC](https://community.thunkable.com/t/executing-and-exit-from-a-continuous-loop-with-the-same-button/2597360/4 "2023-09-15T13:39:04Z")

</div>

I think the solution is not to insert the while loop inside the button click event.  
But I can’t figure out how to execute a routine asyncronously.  
The event should start it, like a function, but don’t have to wait for it to finish to go on.

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [September 15, 2023, 2:58pm UTC](https://community.thunkable.com/t/executing-and-exit-from-a-continuous-loop-with-the-same-button/2597360/5 "2023-09-15T14:58:10Z")

</div>

Are you describing something like a stopwatch? Where you click and it starts counting and then you click again and it stops?

---

<div class="post-metadata">

**Author:** ![Giuseppe\_Geppo\_Catta](https://avatars.discourse-cdn.com/v4/letter/g/2acd7d/32.png) [@Giuseppe\_Geppo\_Catta](https://community.thunkable.com/u/Giuseppe_Geppo_Catta)\
**Post date:** [September 15, 2023, 3:11pm UTC](https://community.thunkable.com/t/executing-and-exit-from-a-continuous-loop-with-the-same-button/2597360/6 "2023-09-15T15:11:24Z")

</div>

Yes, it seems so 😅

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [September 15, 2023, 3:23pm UTC](https://community.thunkable.com/t/executing-and-exit-from-a-continuous-loop-with-the-same-button/2597360/7 "2023-09-15T15:23:15Z")

</div>

Here’s one way to do this:

Project: [Thunkable](https://x.thunkable.com/copy/f83a55f26b81bac257b7cda5f1225502)

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

---

<div class="post-metadata">

**Author:** ![vishruth-ram](https://avatars.discourse-cdn.com/v4/letter/v/82dd89/32.png) [@vishruth-ram](https://community.thunkable.com/u/vishruth-ram)\
**Post date:** [September 16, 2023, 12:23pm UTC](https://community.thunkable.com/t/executing-and-exit-from-a-continuous-loop-with-the-same-button/2597360/8 "2023-09-16T12:23:02Z")

</div>

This should work

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/6/7/677e43a3ab47a16751f524c4f3d34749a98fc768.png)

---

<div class="post-metadata">

**Author:** ![Giuseppe\_Geppo\_Catta](https://avatars.discourse-cdn.com/v4/letter/g/2acd7d/32.png) [@Giuseppe\_Geppo\_Catta](https://community.thunkable.com/u/Giuseppe_Geppo_Catta)\
**Post date:** [September 18, 2023, 6:19am UTC](https://community.thunkable.com/t/executing-and-exit-from-a-continuous-loop-with-the-same-button/2597360/9 "2023-09-18T06:19:55Z")

</div>

Thank you 😉 I was already using Screen Open event But I had some doubts. about using a forever loop inside it. But If a guru like you suggest it, I will follow your suggestion 😉

---

<div class="post-metadata">

**Author:** ![Giuseppe\_Geppo\_Catta](https://avatars.discourse-cdn.com/v4/letter/g/2acd7d/32.png) [@Giuseppe\_Geppo\_Catta](https://community.thunkable.com/u/Giuseppe_Geppo_Catta)\
**Post date:** [September 18, 2023, 6:22am UTC](https://community.thunkable.com/t/executing-and-exit-from-a-continuous-loop-with-the-same-button/2597360/10 "2023-09-18T06:22:44Z")

</div>

In your solution the Start/Stop and Stop variables keep their values inside the repeat until loop, So nobody could change their values and stop the repeat until loop.  
If you are inside a repeat until loop or a while loop or whatever loop, no button click will be triggered.

---

<div class="post-metadata">

**Author:** ![vishruth-ram](https://avatars.discourse-cdn.com/v4/letter/v/82dd89/32.png) [@vishruth-ram](https://community.thunkable.com/u/vishruth-ram)\
**Post date:** [September 18, 2023, 9:52am UTC](https://community.thunkable.com/t/executing-and-exit-from-a-continuous-loop-with-the-same-button/2597360/11 "2023-09-18T09:52:17Z")

</div>

I tried with the `Break out of loop` block but there was an error in thunkable

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/f/0f59f292712368bce16ff80133ae10de8a6f27e8.png) [@system](https://community.thunkable.com/u/system)\
**Post date:** [December 17, 2023, 9:52am UTC](https://community.thunkable.com/t/executing-and-exit-from-a-continuous-loop-with-the-same-button/2597360/12 "2023-12-17T09:52:21Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
