# Creating a clock for time based prospective memory task

**URL:** <https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694>\
**Category:** Builder\
**Tags:** timing\
**Created:** [February 27, 2022, 11:59pm UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694 "2022-02-27T23:59:55Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![elangston](https://avatars.discourse-cdn.com/v4/letter/e/a87d85/32.png) [@elangston](https://discourse.psychopy.org/u/elangston)\
**Post date:** [February 27, 2022, 11:59pm UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694/1 "2022-02-27T23:59:55Z")

</div>

Hi everyone, I am new to psychopy and trying to build a time-based prospective memory task. Specifically, the task should include a clock counting up from that appears for 3 seconds when participants press a certain button, in congruence with a n-back task. I’ve looked at a ton of past posts talking about implementing this clock feature, and right now my coding skills aren’t up to par therefore, I am having trouble integrating the code to fit my study and I would like to try to do this without having to create complex custom code.

Right now I have a trial with n-back words text (set to every repeat), a text clock (set to every frame) which just includes “$t” in the text field, response keys (set to every repeat), and a loop containing my n-back excel document. I also have a rectangle that partially covers up the text clock (right now the clock has too many decimals to be fully covered). I want the study to work so that a rectangle is always covering the clock unless someone presses the spacebar, then it becomes uncovered for 3 seconds.

Here are my current issues

- the clock has way too many digits I want it to just include seconds w/o decimals- how would I do this?
- right now, the experiment is only running if the text clock is set to every frame, although I would like it to be set to every repeat. This is the error message I am getting when I set it to every repeat:

`text_clock.setText(t)`  
`NameError: name 't' is not defined`

- the response keys are only changing the n-back words, I don’t have anything to remove the rectangle from the clock (the rectangle is timed to be on there for infinity), how could I configure psychopy so I have 2 groups of response keys (y/n) and spacebar that correlate to two different stimuli (n-back task and the rectangle)?

Please let me know if you have any advice! Again, I’m very new to psychopy so I apologize if these questions are confusing.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [February 28, 2022, 10:30am UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694/2 "2022-02-28T10:30:01Z")

</div>

> the clock has way too many digits I want it to just include seconds w/o decimals- how would I do this?

round(t)

> the experiment is only running if the text clock is set to every frame

t is the time since the start of the routine, which is why it only works if you set to every frame. If you want a clock that spans across routines, you could create one using `myClock = core.Clock()` and then use `round(myClock.getTime())`

You might find some more answers if you look at my PM Time online demo. I also have a PM N-Back.

> [@PsychoPy Online Demos](https://discourse.psychopy.org/t/psychopy-online-demos/22319):
>
> If you have routines or experiments that are likely to be useful to others, please create a post in this thread, editing your post to add new demos so there is one post per creator. Ideally set your demos so they can be tested without consuming a credit (if you don’t have a site license) by adding a final routine that can’t end, disabling save incomplete results and adding a small number of credits. Please do not discuss the demos in this thread or create more than one post unless you have a la…

---

<div class="post-metadata">

**Author:** ![elangston](https://avatars.discourse-cdn.com/v4/letter/e/a87d85/32.png) [@elangston](https://discourse.psychopy.org/u/elangston)\
**Post date:** [March 1, 2022, 5:10am UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694/3 "2022-03-01T05:10:57Z")

</div>

Thank you! I added a code component with `myClock = core.Clock()` during the begin experiment section of the code. The clock was working for every repeat, although but it was only counting up by 2 seconds (which is the duration of the text\_clock stimuli).

Then I tried to utilize the clock code you provided in your code snippets page. I put code type to both and put `myClock = core.Clock()` in the python side of begin experiment and `myClock = new util.Clock();` in the JS side. I also put `myClock.reset() oldTime=0` in the python side of begin routine, `newTime=myClock.getTime() mins = floor(newTime/60) secs = newTime%60 if newTime != oldTime: text_clock.text=str(int(mins)).zfill(2) + ':' + str(int(secs)).zfill(2) oldTime = newTime`  
in the python side of each frame and `if ((newTime !== oldTime)) { clock_time.text = ((Number.parseInt(mins).toString().padStart(2,"0") + ":") + Number.parseInt(secs).toString().padStart(2,"0")); oldTime = newTime; }` in the JS side of each frame. Finally I put `**text_clock.setAutoDraw(False)**` in the end routine python section. right now I am getting the following error:

`mins = floor(newTime/60) NameError: name 'floor' is not defined`

Do you know where I went wrong?

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [March 1, 2022, 10:17am UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694/4 "2022-03-01T10:17:10Z")

</div>

Hello,

floor is a function from the Python math-library. See here

> **[PsychoPy Python to Javascript crib sheet 2021](https://docs.google.com/document/d/183xmwDgSbnJZHMGf3yWpieV9Bx8y7fOCm3QKkMOOXFQ/edit#heading=h.fbo8f8y1ynwk)**
>
> PsychoPy version 2021.1.3-2021.2.3Python to JavaScript Crib Sheet Compiled by Wakefield Morys-Carter, Oxford Brookes UniversityPlease share widely and follow Psych\_Stats on twitter. PsychoPy 2021 is a major update with several new features. When...

how to deal with this problem.

Your need a code-component at the begin of the experiment which is set to both.

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![elangston](https://avatars.discourse-cdn.com/v4/letter/e/a87d85/32.png) [@elangston](https://discourse.psychopy.org/u/elangston)\
**Post date:** [March 2, 2022, 4:08am UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694/8 "2022-03-02T04:08:43Z")

</div>

Thank you for the demos! I have borrowed from your PM time code and am still not fully there yet: I have gotten the experiment to the point where it counts up on a screen with the n-back words, but haven’t been able to excute the step where pressing the space bar removes a rectangle covering up the clock for 3 seconds. Do you mind taking a look at my code? I thought it would allow me to make the rectangle transparent if the space bar is pressed but currently the rectangle is transparent all of the time even without pressing the space bar. Also, I have two reponse key elements, one for nback response keys that force the end of the routine, and one titled “spaceresponse” that is meant for the clock. Here is my code:

begin experiment:  
`countStarted = False`

begin routine:  
`if not countStarted:`  
` myClock = core.Clock()`  
`countStarted = True`

each frame  
`timeelapsed = myClock.getTime()`  
`if spaceresponse.keys:`  
`if 'space' in spaceresponse.keys:`  
` polygon.setOpacity(0)`  
`else:`  
`polygon.setOpacity(100)`

`if timeelapsed >= 300:`  
` continueRoutine = False`  
` trials.finished = True`  
` countStarted = False`  
`else:`  
` minutes = int(timeelapsed/60.0)`  
`seconds = int(timeelapsed - (minutes * 60.0))`  
` timeText = str(minutes) + ':' + str(seconds)`

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [March 2, 2022, 8:46am UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694/9 "2022-03-02T08:46:40Z")

</div>

Hello,

does this help?

> [@Cannot get opacity to update](https://discourse.psychopy.org/t/cannot-get-opacity-to-update/26121):
>
> For the life of me I cannot get the opacity to update. I have tried using a variable in the opacity box, then updating it in the code. I’ve tried using an interval loop in the Begin Routine and setting the update to set every repeat. I’m just not getting any results. I have this in the Begin Routine: timer = new util.Clock(); Each Frame: if (timer.getTime()\>2 && timer.getTime()\<10) //After 2 seconds, set opacity to 0 { poly…

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![elangston](https://avatars.discourse-cdn.com/v4/letter/e/a87d85/32.png) [@elangston](https://discourse.psychopy.org/u/elangston)\
**Post date:** [March 2, 2022, 9:13pm UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694/10 "2022-03-02T21:13:45Z")

</div>

Hi Jens, working off of that forum post you sent, I set the polygon opacity to 1 and constant, and now I am able to press the space bar once for the polygon to become transparent, however it is staying like that. All of my keys including the backspace are set to every repeat- is this possibly causing the problem? I want at lest two of the keys set to every repeat and if I set the spacebar key to constant, the words stop showing up after the first repeat.

For the code, it would be good to have something like  
`if 'space' in spaceresponse.keys:`  
`polygon.setOpacity(0) for 3 seconds `

obviouly that isn’t the right structure- the post above had the opacity set to 1 every 10 seconds but I would like it to be set to 1 every 3 seconds after its transparency was activated.

edit: I set all aspects of the layout and appearance of the polygon to every repeat- It is kind of working now, however if i press the space bar like 2 seconds into the routine, it becomes opaque agian within one second.

---

<div class="post-metadata">

**Author:** ![elangston](https://avatars.discourse-cdn.com/v4/letter/e/a87d85/32.png) [@elangston](https://discourse.psychopy.org/u/elangston)\
**Post date:** [March 11, 2022, 7:24am UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694/11 "2022-03-11T07:24:20Z")

</div>

I’m now in the process of moving the experiment online and although the code is working locally, I am getting the following error:

`ReferenceError: timeelapsed is not defined`

This is my code  
**Begin Experiment:**  
py  
`countStarted = False`  
js  
`countStarted = false;`

**Begin routine:**  
py  
`if not countStarted:`  
`myClock = core.Clock()`  
`countStarted = True`  
js  
`if ((! countStarted)) {`  
`myClock = new util.Clock();`  
`countStarted = true;`  
`}`  
**Each Frame:**  
py  
`timeelapsed = myClock.getTime()`  
`minutes = int(timeelapsed/60.0) # the integer number of minutes`  
`seconds = int(timeelapsed - (minutes * 60.0))`

`if PMtwobackAspacebar.keys:`  
`if 'space' in PMtwobackAspacebar.keys:`  
`PM2backApolygon.setOpacity(0)`  
`else:`  
`PM2backApolygon.setOpacity(1)`

`if timeelapsed >= 300:`  
`continueRoutine = False`  
`twobackAloop.finished = True`  
`countStarted = False`  
`else:`  
`timeText = str(minutes) + ':' + str(seconds) # create a string of characters representing the time`  
js  
`if (PMtwobackAspacebar.keys) {`  
`if (_pj.in_es6("space", PMtwobackAspacebar.keys)) {`  
`PM2backApolygon.setOpacity(0);`  
`} else {`  
`PM2backApolygon.setOpacity(1);`  
}  
}  
`if ((timeelapsed >= 300)) {`  
`continueRoutine = false;`  
`twobackAloop.finished = true;`  
`countStarted = false;`  
`} else {`  
`timeText = ((Number.parseInt(minutes).toString() + ":") + Number.parseInt(seconds).toString());`  
}

 ![Screen Shot 2022-03-10 at 11.23.48 PM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/e/8/e8f9ed2eb4440bbcd76c8d3bdf75e6bcfd32fba4.png)  
Is there any way to adapt the js code on the “each frame” tab to work on pavlovia?

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [March 11, 2022, 7:31am UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694/12 "2022-03-11T07:31:10Z")

</div>

> [@elangston](#):
>
> timeelapsed

Put timeelapsed = 0 in Begin Experiment

---

<div class="post-metadata">

**Author:** ![elangston](https://avatars.discourse-cdn.com/v4/letter/e/a87d85/32.png) [@elangston](https://discourse.psychopy.org/u/elangston)\
**Post date:** [March 11, 2022, 7:39am UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694/13 "2022-03-11T07:39:20Z")

</div>

Ok- that fixed that error but now it’s giving me a “minutes is not defined” error again. I tried to copy the format of your PM time code on the JS side for the “timeText =” portion - is there something else I’m missing?

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [March 11, 2022, 8:11am UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694/14 "2022-03-11T08:11:27Z")

</div>

In your screenshot it looks like you are using a Both component and have deleted the first three lines of Python code from the JS side.

---

<div class="post-metadata">

**Author:** ![elangston](https://avatars.discourse-cdn.com/v4/letter/e/a87d85/32.png) [@elangston](https://discourse.psychopy.org/u/elangston)\
**Post date:** [March 11, 2022, 8:29am UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694/15 "2022-03-11T08:29:38Z")

</div>

![Screen Shot 2022-03-11 at 12.18.36 AM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/5/c/5cf7cc6af5a10c5d8e9f9a2d2929dc32540767b2.png)

I added “Number.parseInt” to minutes and seconds and deleted the corresponding JS for time elapsed, minutes, and seconds to see if that would help because I saw on your code notes that you didn’t have a corresponding JS for new time, mins, or secs (I’m really not an expert at javascript so I was just kind of moving things around to see if it could work). I tried adding the auto-generated JS code for timeelapssed, minutes, and seconds and I got the following error: " **TypeError: Cannot read properties of undefined (reading ‘getTime’)**"

I have also tried combining your code with a section that would allow me to show and hide a rectangle over the clock:

 ![Screen Shot 2022-03-11 at 12.25.48 AM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/c/4/c4be05c5163d530eb24ce10fe31897963e01f037.png)

but even when I include `import math` for python and `import * as math from 'math';` for JS in the begin experiment tab, I am still getting an error that floor isn’t defined when I run the experiment locally.  
Do you have a suggestion for getting floor to work, or for getting my original adaptation of the code to work?

edit: sorry I just realized that the blank JS columns on your code page mean that the reader should use the automatic python to JS code. However, keeping that in mind I am still having trouble with floor not being defined locally, and the type error for ‘getTime’ online

---

<div class="post-metadata">

**Author:** ![elangston](https://avatars.discourse-cdn.com/v4/letter/e/a87d85/32.png) [@elangston](https://discourse.psychopy.org/u/elangston)\
**Post date:** [March 11, 2022, 5:14pm UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694/16 "2022-03-11T17:14:14Z")

</div>

![Screen Shot 2022-03-11 at 9.10.16 AM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/d/c/dcf9eba312c0ebf7523ce15ebf08c369a3eee6e3.png)

After adding JS to accompany the python side of my code, I am still getting the error: **TypeError: Cannot read properties of undefined (reading ‘getTime’)** developer tools shows that it is occurring on the `timeelapsed = myClock.getTime();` line. How/where would I define “get time” and would this solve this problem?

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [March 11, 2022, 11:00pm UTC](https://discourse.psychopy.org/t/creating-a-clock-for-time-based-prospective-memory-task/27694/17 "2022-03-11T23:00:35Z")

</div>

When you see that there’s a problem with undefined when reading getTime then that suggests that the issue is with myClock not getTime. Did you follow my crib sheet on how to define a clock in JS?
