# End loop after 5 minutes

**URL:** https://discourse.psychopy.org/t/end-loop-after-5-minutes/35335
**Category:** Builder
**Created:** [June 11, 2023, 11:41pm UTC](https://discourse.psychopy.org/t/end-loop-after-5-minutes/35335 "2023-06-11T23:41:56Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![hkim174](https://avatars.discourse-cdn.com/v4/letter/h/278dde/32.png) [@hkim174](https://discourse.psychopy.org/u/hkim174)
#### Post date: [June 11, 2023, 11:41pm UTC](https://discourse.psychopy.org/t/end-loop-after-5-minutes/35335/1 "2023-06-11T23:41:56Z")

</div>

**OS** (e.g. Win10): Win10  
**PsychoPy version** (e.g. 1.84.x): v2022.02.05  
**What are you trying to achieve?:** Ending loop after 5 minutes

Hi.

My participants are shown 30 pictures and then are asked to recall them by describing each picture in each textbox. I made a loop that includes nRep of 30 textboxes, but I want this loop to end when it reaches 5 minutes.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/3/f/3ff1c23e4e705a1924ec06250134dd4d36765aff.jpeg)

I don’t know how to code, but here is what I’ve done so far with the help of other topic. (the name of the loop is trial)

**Begin Experiment**  
newClock = core.Clock()

**Begin Routine**  
newClock.reset()

**Each Frame**  
if newClock.getTime() \>= 300:  
trial.finished = True

However, it seems like the loop is still ending when all the 30 textbox routines are done, not after 5 minutes.

Some help would be much appreciated!

---

<div class="post-metadata">

### Author: ![shabkr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/shabkr/32/32052_2.png) [@shabkr](https://discourse.psychopy.org/u/shabkr)
#### Post date: [June 12, 2023, 6:24pm UTC](https://discourse.psychopy.org/t/end-loop-after-5-minutes/35335/2 "2023-06-12T18:24:51Z")

</div>

Hi @hkim174,

The code you’ve written looks correct but might be in the wrong place. With `clock.reset()` in `Begin Routine`, your clock will reset with every trial and will never reach 300 seconds. You need two components, one to create & start the clock, the other to check the clock.

**Component 1** : Create and start the clock

- this component should be in the routine **immediately before your loop starts** (_e.g._ your loop is `trial` so you want Component 1 to be in `blank_1000ms`

- this routine should contain the following code:

**Component 2** : Check the clock

- this component should be in the routine **inside the loop** (_e.g._ your loop is `trial` so you want Component 2 to be in `Recall1`
- this trial should contain the following code:
  - **Each Frame**

```auto
if newClock.getTime() >= 300:
trial.finished = True

```

See also: [Ending an entire experiment after a set time (60 minutes)](https://discourse.psychopy.org/t/ending-an-entire-experiment-after-a-set-time-60-minutes/34794)

Hope that helps,  
-shabkr

---

<div class="post-metadata">

### Author: ![hkim174](https://avatars.discourse-cdn.com/v4/letter/h/278dde/32.png) [@hkim174](https://discourse.psychopy.org/u/hkim174)
#### Post date: [June 13, 2023, 9:55pm UTC](https://discourse.psychopy.org/t/end-loop-after-5-minutes/35335/3 "2023-06-13T21:55:10Z")

</div>

Thank you so much, Shabkr! After three days of struggling, it finally worked!

An error message popped up regarding the indentation so I added an indent to the second line (trial.finished = True).

if newClock.getTime() \>= 300:  
trial.finished = True

---

<div class="post-metadata">

### Author: ![hkim174](https://avatars.discourse-cdn.com/v4/letter/h/278dde/32.png) [@hkim174](https://discourse.psychopy.org/u/hkim174)
#### Post date: [June 16, 2023, 2:27am UTC](https://discourse.psychopy.org/t/end-loop-after-5-minutes/35335/4 "2023-06-16T02:27:49Z")

</div>

Hi @ [shabkr](https://discourse.psychopy.org/u/shabkr),

On top of the experiment I had, I added a question to make sure that the participants are still engaging in my task. (e.g., click 5 to indicate that you are still engaging in the task). So there are now two things in my bigger loop. (see the picture I attached)

1. a loop (named trials\_2) within a bigger loop(named trial\_3)
2. a routine(named attention) within the same bigger loop(named trials\_3)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/1/4/14902a12a6030185ae8bc506ec01bff8760cd6a7.png)

Like your previous explanation, I’ve added a **code just before the bigger loop** (blank\_code) and put

**Begin Experiment**  
newClock = core.Clock()

**End Route**  
newClock.reset()

To **Each Frame** in both practicerecall and attention  
I have added  
**if newClock.getTime() \>= 300:**  
**trials\_3.finished = True**

However, this does not work. Obviously, I don’t know what I am talking about here, but I would really really appreciate your help.

---

<div class="post-metadata">

### Author: ![shabkr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/shabkr/32/32052_2.png) [@shabkr](https://discourse.psychopy.org/u/shabkr)
#### Post date: [June 16, 2023, 5:13pm UTC](https://discourse.psychopy.org/t/end-loop-after-5-minutes/35335/5 "2023-06-16T17:13:17Z")

</div>

Hi @hkim174,

Right now your code will end the trials\_3 loop, but will keep running the trials\_2 loop, even if the participant goes over time. Try adding `trials_2.finished = True` in your if-statements to make sure both trial loops end.

-shabkr

---

<div class="post-metadata">

### Author: ![hkim174](https://avatars.discourse-cdn.com/v4/letter/h/278dde/32.png) [@hkim174](https://discourse.psychopy.org/u/hkim174)
#### Post date: [June 16, 2023, 5:52pm UTC](https://discourse.psychopy.org/t/end-loop-after-5-minutes/35335/6 "2023-06-16T17:52:47Z")

</div>

Hi @shabkr

I added the if statements, however, the loop still continues after 300 seconds.  
Does the number of nReps in each loop matter in this case?

I have 3 nReps for loop named trials\_2  
& have 2 nReps for loop named trials\_3.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![shabkr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/shabkr/32/32052_2.png) [@shabkr](https://discourse.psychopy.org/u/shabkr)
#### Post date: [June 16, 2023, 7:36pm UTC](https://discourse.psychopy.org/t/end-loop-after-5-minutes/35335/7 "2023-06-16T19:36:18Z")

</div>

The nReps shouldn’t matter, I tried building a minimal experiment in Psychopy 2022.2.5 and the loops correctly ended early.  
**Each Frame** of both practicerecall and attention should have:

```auto
if newClock.getTime() >= 300:
    trials_2.finished = True
    trials_3.finished = True

```

It sounds like everything should be working, so without seeing the whole experiment I am not sure the issue might be.

Here is a small example, it is set up to end early after 14 seconds. Matching your case, the small loop has 3 nReps and the large loop has 2 nReps.

All routines in this example take 3 seconds, so it ends after completing 3 trial routines + 1 attention routine + 1 trial routine (the end early code is triggered here) + 1 attention routine. Extra code is required to skip/interrupt routines, which is why that last attention routine runs.  
[endloopearly.psyexp](https://discourse.psychopy.org/uploads/short-url/3MleFQid6XXcUjpJBtnmyUuEH29.psyexp) (18.5 KB)

---

<div class="post-metadata">

### Author: ![hkim174](https://avatars.discourse-cdn.com/v4/letter/h/278dde/32.png) [@hkim174](https://discourse.psychopy.org/u/hkim174)
#### Post date: [June 17, 2023, 5:10am UTC](https://discourse.psychopy.org/t/end-loop-after-5-minutes/35335/8 "2023-06-17T05:10:43Z")

</div>

Hi @shabkr,

Thanks for such a sophisticated reply. This is very thoughtful.  
I did try your code from your sample experiment, however, it does not work on mine.

I played around with your sample experiment thinking about what would be the cause, and I think it is the time set for each routine that’s causing the error when it’s applied to my experiment. Your trials routines were set to 3 seconds (when I removed this, your sample experiment the screen would not move on)

However, for mine, participants have to type in texts and click a button for the screen to move on (for the loop to continue)

I attached a brief version of my experiment for you to see.  
(fyi, the loop is not working either, and I am not sure why)

Some help would be much appreciated!  
[ending loop after X time.psyexp](https://discourse.psychopy.org/uploads/short-url/iA8UHTwZ5q1UFMAZcd6QpMZ5H65.psyexp) (18.6 KB)

---

<div class="post-metadata">

### Author: ![shabkr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/shabkr/32/32052_2.png) [@shabkr](https://discourse.psychopy.org/u/shabkr)
#### Post date: [June 19, 2023, 6:02pm UTC](https://discourse.psychopy.org/t/end-loop-after-5-minutes/35335/9 "2023-06-19T18:02:15Z")

</div>

Hi @hkim174,

Thanks for sharing your demo.

> [@hkim174](#):
>
> (fyi, the loop is not working either, and I am not sure why)

In the mouse component, make sure the `new clicks only` box is checked. This prevents of the previous trial from carrying over to the next trial (which causes it to immediately end).

After fixing the mouse component to be new clicks only, when I inserted the code into your demo experiment, the loop ends early as expected.  
[ending loop after X time.psyexp](https://discourse.psychopy.org/uploads/short-url/ouMtuTjvEJwbNhr7oDY7nX25s00.psyexp) (23.5 KB)

In your main experiment are you receiving any specific error messages? What is specifically going wrong with the main experiment right now?

-shabkr

---

<div class="post-metadata">

### Author: ![hkim174](https://avatars.discourse-cdn.com/v4/letter/h/278dde/32.png) [@hkim174](https://discourse.psychopy.org/u/hkim174)
#### Post date: [June 26, 2023, 5:37am UTC](https://discourse.psychopy.org/t/end-loop-after-5-minutes/35335/10 "2023-06-26T05:37:35Z")

</div>

Hi @shabkr,

I think I might have figured out what the issues are here.

I have a “BIG loop” that I want to terminate after 14 seconds, regardless of which routines I am currently executing. As you know the two routines are the “response routine,” which consists of 3 nReps, and the “attention routine,” which has 2 nReps.

However, I noticed that when you spend more than a certain amount of time (e.g., say more than 14 seconds) in one of the “response routines”, and then click the next button, the routine transitions to the “attention routine” and then ends the BIG loop.

(I used the recent file that you attached to the thread)

Could it be possible that the code in the “Response routine” and the “Attention routine” are not compatible?

As always, thanks so much in advance!

---

<div class="post-metadata">

### Author: ![shabkr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/shabkr/32/32052_2.png) [@shabkr](https://discourse.psychopy.org/u/shabkr)
#### Post date: [June 26, 2023, 5:25pm UTC](https://discourse.psychopy.org/t/end-loop-after-5-minutes/35335/11 "2023-06-26T17:25:32Z")

</div>

Hi @hkim174,

> [@hkim174](#):
>
> I noticed that when you spend more than a certain amount of time (e.g., say more than 14 seconds) in one of the “response routines”, and then click the next button, the routine transitions to the “attention routine” and then ends the BIG loop.

Yes, this is intended behaviour with the current code. `LOOP.finished = True` will prevent a loop from repeating, but does not skip/interrupt any routines that still need to run. There are a couple options here depending on the behaviour you are looking for.

In both cases, the code looks like this:

```py
# CLOCK_COMPONENT: the name of your clock component
# TIME: the max time you want the loops/routines to run for
# LOOP: the name of the loop that needs to end early
if CLOCK_COMPONENT.getTime() >= TIME:
    LOOP.finished = True #add this line for each loop that needs to end early
    continueRoutine = False #add this line to end the current routine early

```

## Option 1: Skip the Routine

If you want to skip the attention routine _before it starts_ you would add the code to **Begin Routine** in the attention routine’s code component. **I recommend this option and have attached an example.** (in the example, advance the trials with the spacebar)  
[endloopearly.psyexp](https://discourse.psychopy.org/uploads/short-url/hGaouJ0QsRSdZAyxOcYmoYJyiYb.psyexp) (22.1 KB)

## Option 2: Interrupt the Routine

If you want to stop the attention routine _while it is running_ you would add your code to **Each Frame** in the attention routine’s code component.

Hope that helps,  
-shabkr

---

<div class="post-metadata">

### Author: ![hkim174](https://avatars.discourse-cdn.com/v4/letter/h/278dde/32.png) [@hkim174](https://discourse.psychopy.org/u/hkim174)
#### Post date: [July 4, 2023, 3:55am UTC](https://discourse.psychopy.org/t/end-loop-after-5-minutes/35335/12 "2023-07-04T03:55:34Z")

</div>

I decided to go with option #1. Thanks for your patience and input on this. This helped me A LOT. I cannot thank you enough, @shabkr!
