What is the 'scope' of a clock? Also - does using 'both' mean i now have to code JS?

Hi everyone.

I would like to start a clock at the beginning of my experiment and then at certain points throughout the experiment - write the current time of that clock to my output file. Hence I would be able to see, ‘oh this participant started this section very quickly’ etc… as a seperate thing to the RTs of specific trials.

(Oh and I have disabled all the ‘save offset.onset’ times for objects because I found these just cluttered up my output file enormously - and I would like to control what I log.

So. I created a clock in the ‘Begin experiment’ tab of an early routine in my experiment.

ExpClock = core.Clock()

And then added this line to the ‘Each frame’ tab (of the same routine):

ExpClockText.text = ExpClock.getTime()

Which all works just fine - the text object dynamically updates to show the elapsed time. Nice.

I then tried adding a similar textobject to subsequent routines and added the following to the text field of these objects within builder:


I was expecting to see the elapsed time when the routine begins (not dynamic) on this text object for any subsquent screens and then I would begin to think about capturing this variable and writing it to my output file when I wanted to.


Instead for these subsequent screens, the value displayed is always short - like a second or so - and the same for every subsequent screen - as if the clock were being reset somehow.

So my question is - how can I get a clock to begin early on and be able to reference it subsequently in different routines to see elapsed time?

Follow up question: I then tried uploading this experiment to Pavlovia - which threw an error so I looked at @wakecarter’s cribsheet - and saw that I need to use differnet syntax for clocks in the JS panel. So I did this and it worked - BUT… at this point - the two panels become disconnected right? So if I want to do ANYTHING listed on that crib sheet - I from that point forth need to update the code in BOTH panels. Is that right? Meaning at this point - I need to learn Javascript as well? Am I getting this wrong - coz that would be… er… less than desirable.

Thanks for any pointers on either of these two issues.



Hi Dan,

if I’m not mistaken there should already be a clock created at the begin of the experiment named “globalClock” to track the time since the experiment started. This clock should be automatically created by psychopy.

I suggest you create a variable in each routine you want to monitor and to add a text component with that variable as text:

#in the each frame tab
timeVar = globalClock.getTime() #add $timeVar as the text of a textcomponent

As for coding I am afraid you are mostly correct. The best way to do it if you are not familiar with JS is to write the code in Python, use the auto translation and then correct the JS code with the cribsheet.

Many of my routines contain two code components – one being set to Auto translate and the second set to Both. Since I prefer to code in Python, then I cope the JS version of manual edits I need to make to the Python side as comments. This means that if I want to add to the Python code (and can’t add it to a separate Auto component) then I can switch back to Auto, add the code, switch back to Both and finally copy the code from my comments back to the JS side.

Hi Tandy.

I’m afraid I have not been able to get this to work for me.

If I add a text component and then in the ‘text’ attribute of that component add:


Then I get the error message:

NameError: name 'globalClock' is not defined

I have also tried assigning this to a variable, spitting it at the console output etc… with no success.



When you did this, was the text object set to update each repeat, or constant?

Since ExpClockText.text = ExpClock.getTime() worked in the Each Frame tab when you wanted a dynamic clock, I’d recommend using the same code in Begin Routine when you want a static clock. You could even use ExpClockText,setAutoDraw(True) if you want to reuse the text component from your previous routine.

ExpClock shouldn’t reset from one routine to the next, unless you tell it to or recreate it, so I think something else must be going on here.