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
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.
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:
newClock = core.Clock()
I recommend end routine to start the clock and match it closely with when the loop starts
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:
if newClock.getTime() >= 300:
trial.finished = True
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)
a loop (named trials_2) within a bigger loop(named trial_3)
a routine(named attention) within the same bigger loop(named trials_3)
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.
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:
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 (18.5 KB)
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)
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?
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:
# 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 (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.