How to Pause a loop?

In my experiment, I have two loops named: trials_Practice & trials_Main.
trials_Main will be run around 70 to 77 times. In this loop, I want to introduce a break when half of the total times of the loop (around the completion of 35 trials) has been completed. I want to present a text component saying “2 minutes Break” after completing half of the main trials with ‘Press any key to continue’ as a response.

I am unable to figure out where I will write the code in the builder view of code component. I tried to do this to introduce a count variable and give start time condition into the text component. It didn’t work.
I know there is some way to pause a loop but I didn’t find out.
Could anyone have experienced the same problem in the past? Or anyone can add the link with the same problems’ answer.

OS (e.g. Win10):
PsychoPy version (e.g. 1.84.x):

Create a second routine containing your pause text and keyboard component. Insert a code component on that routine. In its “begin routine” tab, put something like this so that routine will run only when exactly 35 trials (counting from 0) have been completed:

if trials_Main.thisN != 34:
    continueRoutine = False
1 Like

Dear Michael,

This works perfect, but in my case I would like to make pause several times (e.g., after each of 25 trials, total of 250 stimuli will be presented). With this code only one pause have been made (after 25 trials and that is all. Should I use thisRepN instead of thisN?

Divide the current trial number by 25 and see if the remainder is zero (but don’t pause on the first (zeroth) trial):

if your_loop_name.thisN > 0 and your_loop_name.thisN % 25 != 0:
    continueRoutine = False

I’m having a similar issue. I have a basic study where I want to present 100 images in a random order. However, I want to have 2 blocks of 50 images each with a screen after the first 50 trials that says “You may now take a break” then have the participant press a key to resume the study. I would like to have my images presented in a random order for each participant and not repeat in the 2 blocks (so 100 images total with no repeats). I currently have all of my image names in 1 column in an excel file called Stimuli.xlsx and the column they are in is called Image. I tried your suggestion to create a routine that contains my pause text, keyboard component and code component that has the code you suggested. However, all it does it show the break routine after each trial and not after 50 trials. I am using PsychoPy version 3.2.4. I’d be very appreciative of your help.

Thank you!

1 Like


I am not expert on this but I had a similar issue and it worked for me.
Please paste this code in begin routine block of code component. Place code component at the end of your condition loop.

if trials_Main.thisN != 50:
    continueRoutine = False


Hi Samyak,

Thank you for your response. I have tried this and this is what doesn’t work. It just creates the break screen after every trial and not after 50.


Could you provide the screen shot of code component along with flow?


Yes! Here are the images.


It seems right to me. The block design is mostly similar to mine so please check the image of trial loop, you might get something.

Annotation 2020-08-15 011711|690x350


I actually just fixed my issue and the code needed to be on the Each Frame box and not on Begin routine!


That was a bug that re-surfaced around version 3.2.0. It was fixed late last year. If you are creating a new experiment, it would be worth updating to a later release.

Hi Michael, thank you for the suggestion. I’ve downloaded the newest release of psychopy and the code still only works in the Each Frame box.

That’s very strange. I’d be interested to see your .psyexp file if you are happy to share it. If this issue has arisen again, it really needs to be fixed for everybody (as having this code only run in the “each frame” tab can cause a single frame glitch where the very first frame of the unwanted routine will always display).

Hi Michael, I’m having the same issue. Break works find locally, shows up every 5 trials, but online it shows up after every trial. My PsychoPy version is 2020.2.4. I’ve tried on both Mac and Windows, same problem shows up both places. I’ve tried to put the code snippet in Begin Routine and Each Frame but makes no difference, it seems that online it just skips the conditional statement. Can you help me figure out why? The project gitlab is

Sorry, the JavaScript side of things is not my forté. But I think there are a few other active topics on this at the moment, where hopefully a resolution will be found?

Hi, I also have the similar problem of displaying the break session online. Have you find any solution to solve this?

Okay, so I finally resolve my problem by following this code here Code Component not working when running in Pavlovia
hope this can help people with similar situation.

Hey, I didn’t figure this out until you posted your solution. The linked JS code worked for me as well, thanks!

Hi Michael,
I recently came across this thread as I am also looking to insert a break every 40 trials in my experiment (80 unique trials that are repeated twice for a total of 160 trials) I inserted this code into my “begin routine” component:

It does successfully give a break every 40 trials, however it also pauses for the break after the first trial as well. I thought that the initial “trialsN > 0” would prevent this from happening, but it doesn’t seem like it did. Any ideas of why this is/what I can do to fix this?
Thanks so much and let me know!