# Exit button --\> end loop

**URL:** <https://discourse.psychopy.org/t/exit-button-end-loop/10472>\
**Category:** Builder\
**Created:** [January 7, 2020, 9:47am UTC](https://discourse.psychopy.org/t/exit-button-end-loop/10472 "2020-01-07T09:47:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aelreym](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@aelreym](https://discourse.psychopy.org/u/aelreym)\
**Post date:** [January 7, 2020, 9:47am UTC](https://discourse.psychopy.org/t/exit-button-end-loop/10472/1 "2020-01-07T09:47:50Z")

</div>

If this template helps then use it. If not then just delete and start from scratch.

**OS** (e.g. Win10): macOS Catalina Version 10.15.1  
**PsychoPy version** (e.g. 1.84.x): Psychopy v.3.1.5  
**Standard Standalone? (y/n)** If not then what?: mac

**What are you trying to achieve?:** exit the loop after clicking on exit button  
In my experiment I have two routines in the same loop (you can imagine it as two trials with different levels of difficulty, trial1 and trial2, “trials” is the name of the loop) and after that, I have one ‘Feedback’ routine. In both trials you have an “exit” button on which you can click if you want to stop the trials and go directly to the feedback routine.

**What did you try to make it work?:** I have the following code for the exit button:

in the tab “Begin routine”:  
ExitButton = visual.ButtonStim(win, labelText= “Exit”, pos=(0, -.28))  
ExitButton.buttonEnabled = True

in the tab “each frame”  
ExitButton.draw()  
if ExitButton.buttonSelected:  
continueRoutine = False  
trials.finished = True

**What specifically went wrong when you tried that?:** This code works in the seconde routine (the one just before the feedback routine) but not in the first one. If I click on the exit button in the first routine I only end the routine and go to the next routine (trial2) but it does not quit the loop and go to the feedback routine.

---

<div class="post-metadata">

**Author:** ![Oceane](https://avatars.discourse-cdn.com/v4/letter/o/8e8cbc/32.png) [@Oceane](https://discourse.psychopy.org/u/Oceane)\
**Post date:** [February 12, 2020, 1:42pm UTC](https://discourse.psychopy.org/t/exit-button-end-loop/10472/2 "2020-02-12T13:42:03Z")

</div>

Hey @aelreym, same problem here, did you figure things out?  
Océane.

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [February 12, 2020, 1:44pm UTC](https://discourse.psychopy.org/t/exit-button-end-loop/10472/3 "2020-02-12T13:44:52Z")

</div>

@Oceane. @aelreym, the method to end loops online has been fixed in version 2020.1, and the correct method for ending loops using JavaScript code is the following - in this example we end the loop when the current loop iteration (nReps) is 2:

```auto
// this conditional can be whatever you want, this is just an example
if (currentLoop.thisRepN == 2)   
  {
    trials.finished = true;
  }

```

Note, this will only work from PsychoPy version 2020.1 onwards.

---

<div class="post-metadata">

**Author:** ![aelreym](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@aelreym](https://discourse.psychopy.org/u/aelreym)\
**Post date:** [February 18, 2020, 12:04pm UTC](https://discourse.psychopy.org/t/exit-button-end-loop/10472/4 "2020-02-18T12:04:15Z")

</div>

@dvbridges, I did the update and now I cannot open psychopy anymore. When I want to open psychopy it shows me an error message and “open console” or “terminate”.

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [February 18, 2020, 1:21pm UTC](https://discourse.psychopy.org/t/exit-button-end-loop/10472/5 "2020-02-18T13:21:18Z")

</div>

@aelreym, try the [troubleshooting](https://www.psychopy.org/troubleshooting.html) suggestions, you may just need to clear your user and app data config files.

---

<div class="post-metadata">

**Author:** ![aelreym](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@aelreym](https://discourse.psychopy.org/u/aelreym)\
**Post date:** [February 18, 2020, 5:22pm UTC](https://discourse.psychopy.org/t/exit-button-end-loop/10472/6 "2020-02-18T17:22:57Z")

</div>

@dvbridges, I was not able to resolve this with the troubleshooting suggestions but I don’t understand every suggestion there. But in the terminal program I pasted the following message: /Applications/PsychoPy3.app/Contents/MacOS/python -m psychopy.app.psychopyApp  
AND I got this error message: /Applications/PsychoPy3.app/Contents/MacOS/python: Error while finding module specification for ‘psychopy.app.psychopyApp’ (ModuleNotFoundError: No module named ‘psychopy’)  
Do you know what I should do?
