# End loop after time specific routine

**URL:** https://discourse.psychopy.org/t/end-loop-after-time-specific-routine/35243
**Category:** Coding
**Created:** [June 5, 2023, 6:11am UTC](https://discourse.psychopy.org/t/end-loop-after-time-specific-routine/35243 "2023-06-05T06:11:53Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Lalaland1](https://avatars.discourse-cdn.com/v4/letter/l/8797f3/32.png) [@Lalaland1](https://discourse.psychopy.org/u/Lalaland1)
#### Post date: [June 5, 2023, 6:11am UTC](https://discourse.psychopy.org/t/end-loop-after-time-specific-routine/35243/1 "2023-06-05T06:11:53Z")

</div>

Hi y’all.  
I want to end loop after two minutes but starting the timer in a specific routine.

I try globalclock but apparently does not work in my case.

Any help is appreciated

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [June 5, 2023, 9:35am UTC](https://discourse.psychopy.org/t/end-loop-after-time-specific-routine/35243/2 "2023-06-05T09:35:02Z")

</div>

Create a clock object in your Begin Experiment code, and just reset it during the routine where you want to start it. If you put an object in the “Begin Experiment” code it has global scope for the experiment. Just make sure you only check it after the routine where you reset it.

---

<div class="post-metadata">

### Author: ![Lalaland1](https://avatars.discourse-cdn.com/v4/letter/l/8797f3/32.png) [@Lalaland1](https://discourse.psychopy.org/u/Lalaland1)
#### Post date: [June 6, 2023, 12:54am UTC](https://discourse.psychopy.org/t/end-loop-after-time-specific-routine/35243/4 "2023-06-06T00:54:45Z")

</div>

Thanks for answering.  
Since I’m new to psychopy maybe I don’t fully understand your answer.  
In code component I have  
BEGIN EXPERIMENT  
globalClock.getTime()  
startTime = globalClock()  
BEGIN ROUTINE  
globalClock.reset()  
END ROUTINE  
if globalClock.getTime() - startTime \>= 120:  
trials\_2.finished = True  
continueRoutine = False

However, now it won’t even open the experiment.

---

<div class="post-metadata">

### Author: ![Lalaland1](https://avatars.discourse-cdn.com/v4/letter/l/8797f3/32.png) [@Lalaland1](https://discourse.psychopy.org/u/Lalaland1)
#### Post date: [June 6, 2023, 3:30am UTC](https://discourse.psychopy.org/t/end-loop-after-time-specific-routine/35243/5 "2023-06-06T03:30:33Z")

</div>

As well I tried:  
END ROUTINE  
if t\>=4:  
trials\_2.finished=True  
continueRoutine = False  
But this only cut the routine after 4 seconds of not receiving any clicks.

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [June 6, 2023, 5:28am UTC](https://discourse.psychopy.org/t/end-loop-after-time-specific-routine/35243/6 "2023-06-06T05:28:09Z")

</div>

The second one makes perfect sense to me, t is a built-in timer.

For the first one I was thinking something like this. Note that I am using the **before experiment** tab. This is very important.

**Before** experiment:

```auto
newClock = core.Clock()

```

Begin routine:

```auto
newClock.reset()

```

End routine:

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

```

(You shouldn’t need continueRoutine since the routine has already ended, but if you want it to end the routine, put this code in the “each frame” tab instead)

What happens with this setup?

---

<div class="post-metadata">

### Author: ![Lalaland1](https://avatars.discourse-cdn.com/v4/letter/l/8797f3/32.png) [@Lalaland1](https://discourse.psychopy.org/u/Lalaland1)
#### Post date: [June 6, 2023, 8:18am UTC](https://discourse.psychopy.org/t/end-loop-after-time-specific-routine/35243/7 "2023-06-06T08:18:58Z")

</div>

Actually, this only cut the routine after 120 seconds of not receiving any clicks, if the participant continues typing, then the routine keeps going.

I want to cut even if the participant continues typing.

I’m sorry for explaining this so far.

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [June 6, 2023, 3:07pm UTC](https://discourse.psychopy.org/t/end-loop-after-time-specific-routine/35243/8 "2023-06-06T15:07:38Z")

</div>

Ah, then just put the “end routine” code in the “each frame” tab instead and add `continueRoutine = False` back into it.

---

<div class="post-metadata">

### Author: ![Lalaland1](https://avatars.discourse-cdn.com/v4/letter/l/8797f3/32.png) [@Lalaland1](https://discourse.psychopy.org/u/Lalaland1)
#### Post date: [June 7, 2023, 2:09am UTC](https://discourse.psychopy.org/t/end-loop-after-time-specific-routine/35243/9 "2023-06-07T02:09:43Z")

</div>

Still the same situation.

Apparently nothing changes. It only ends the routine after no clicks, if I keep typing, the routine continues.

Thanks for the help.

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [June 7, 2023, 7:18am UTC](https://discourse.psychopy.org/t/end-loop-after-time-specific-routine/35243/10 "2023-06-07T07:18:13Z")

</div>

That’s interesting. Could you share your .psyexp file here? It’s possible that PsychoPy has some code forcing the routine to continue as long as its receiving input, but I need to see how the trial is put together to figure it out.
