| Reference | Downloads | Github

Closing a loop with a temporal criteria


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

OS (Sierra 10.13.6):
PsychoPy version : Psychopy 2 PY 3 1.90.1


I developed a task on psycho-py where some stimuli are presented, and if the participant reaches a number bigger than one variable (e.g., total_correct), there is code added that ends the loop (End routine). It is working nice.

My question is: Is it possible to set temporal criteria to finish this loop? For example, after 60 seconds this trials started the loop will finish?

I bought the book recently released and search the manuals available, but I couldn’t find information about using the time elapsed in an experiment this way. Does anyone know where I could find information about the mechanics and correct codes to use the time elapsed in an experiment?

Thanks a lot!


In the “every frame” tab of a code component:

if t >= 60.0:
    continueRoutine = False
    yourLoopName.finished = True


Hello Michael, thank you very much for your reply. I am sorry for responding only now, I was participating on a conference last week, and had no time on the computer.

The code you said worked perfectly thank you. However I would like to know if there is a variable that consider the entire time of the experiment and not only the time in a trial.

The way it is now, if there is any trial and it lasts for 60 seconds it ends. But is there any variable that I could show many different trials and it will consider the complete time since the experiment started? Or even it counts the time of my loop?

Thank you very much again,

Best wishes,



You could create one with any of the clock/time/core functions. For example, at the start of your experiment (or at any point where you want to start counting, you can put something like:

timeStart = psychopy.core.getTime()

Then in the every frame* of your routine bit, you could write:

timePassed = psychopy.core.getTime() - timeStart
# This gives you the time that has passed since you created the timeStart variable.

if timePassed >= 60.0:
    continueRoutine = False
    yourLoopName.finished = True
# This bit is borrowed from Michael's post above. I THINK that getTime() uses seconds rather than milliseconds, but if not, adapt accordingly.

*You might want to put it at “end routine” instead, so that a) the calculation doesn’t execute every frame and b) your current trial finished neatly. But that’s up to you!


Hey Cesco, thank you very much for your suggestion it worked really nice.

however when I put the code timeStart = psychopy.core.getTime() it was showing an error saying

Name error: name psychopy is not defined. I tried to remove the psychopy and use only the timeStart = core.getTime() and it worked great.

I would like to ask another question if you have any clue on how to do it. I have several stimuli that will be presented this way for 60 seconds. Even if I created different timeStarts (e.g., timeStart1, timeStart2, etc) and different timePassed (e.g. timePassed1, timePassed2, etc) the value of the core.getTime() seems to pick the time of the beginning of the experiment. What I am doing now is to use multiples of 60 (1st trial 60, 2nd trial 120, 3rd trial 180, etc). Do you know any way that the core.getTime() gets the time of the moment of the beginning of the routine and not the beginning of the experiment?

Thank you very much for your reply!


Additionally I noticed that the time criterion is working but only if a correct responses occurs. If there is no response the routine is not being finished. If I disable the force the end of routine in the key_resp that I inserted only the time criterion is not controlling the end of my loop. If you have any advice on this just let me know. Thanks so much! Sorry to bother!


Hi, to create a clock you should use

timeStart = core.Clock() # to instantiate a clock object
timeStart.reset()   # To reset time at your baseline e.g., start of trial, start of stimuli
timeStart.getTime()  # to get the time

BTW, if you are using Builder, there a variable called ‘globalClock’ which is created at the beginning of the experiment. Thus, if you wanted a clock that considers the complete time since the experiment started, this is it. Just use globalClock.getTime() whenever you need it.

For controlling the end of the loop using a response, you can use something similar to what Michael has shown, in the Every Frame tab of the code component:

# Every Frame 
if <yourKeyboard>.keys == corrAns:  # where corrAns = 'space'
    continueRoutine = False
    yourLoopName.finished = True


Hi dvbridges,

Thank you very much for your reply. I will use the codes you posted and post the results later.

About the end of the loop, I might expressed unclearly (sorry, as you can see I am not a native English speaker) I would like to do the exact opposite, I would like that the only criterion is the time, so if the end routine code have a line, if time Passed >= 5.0, if 5.0 seconds are passed even without any response the loop should end.

however what is happening is that, the stimulus is presented on the screen and even if 5 seconds passed the routine only ends if I press the key. Just let me know if I explained correctly now. Thank you very much!!!


Ok, so you only want the loop to end after 5 seconds, not on a response?


Hi dvbridges,

I would like some events occur after some time and a response. This is working well, however if no response happen I would also need that after some time only criteria the loop would end.

I did the code including time AND response and this part is fine. (e.g >=5 and a correct response).

However if I use also another code considering only time (e.g timePassed >= 10) it is not ending the loop. It stays active infinitely and it is only ending if I press the correct key.

I was thinking that if any of the statements were true they should work .

Do you have any advice?

Thank you very much!!!


Ok, well something like the following should work:

if <yourKeyboard>.keys == corrAns and t >= 5:
    continueRoutine = False
    yourLoopName.finished = True

elif <yourKeyboard>.keys != corrAns and t >= 10:
    continueRoutine = False
    yourLoopName.finished = True

Attached is a Builder file to help you debug - note this was written in the latest version of PsychoPy conditionalEnd.psyexp (9.1 KB)


Hey dvbridges,

Thank you very much for your attention, with your advices (and also Cesco and Michael) I managed to finish my task and it is working really nice.

I couldn’t use the reset resource, but I figure out another way without using it. I don’t want to bother more but if you have any suggestion on written material about psychopy (other than the book and the manual) just let me know.

Thank you very much!