| Reference | Downloads | Github

Change value of 't' for participants who do not respond immediately

Hello all!

I was wondering if there was a way to change the value of t depending on when a participant responds.

The task I have set up is as follows:

  • Participant sees a square oscillate on screen
  • Participant then sees a thin rectangle that will grow depending on how long the space key is held. This is done by the calculation: sq.width = (10 * (t * 5)) where sq is the object to be grown.

Currently, if a participant decides not to press space for let’s say, 5 seconds, the square will initialize at (10*(5*5)) and then grow accordingly.

I have tried something such as:

new_t = t*0 however this clearly just makes new_t = 0 and the shape will not update

I have also tried sq_key.clock.reset();, but this does not work either.

Any advice would be greatly appreciated, thank you!

Hello David,

could you please show the code that you are using to let the rectangle grow and shrink? It is also helpful to include when in the routine you are using the code.

Best wishes Jens

Hi Jens, sure thing!:

Begin Experiment

sq_key = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});

Begin Routine

sq_key.keys = undefined;
sq_key.rt = undefined;
sq_key.duration = undefined;
sq_key_allKeys = [];
sq.width = 10;


Each Frame

let theseKeys = sq_key.getKeys({keyList: ['space', 'right'], waitRelease: false, clear: false});
sq_key_allKeys = sq_key_allKeys.concat(theseKeys);

if (sq_key_allKeys.length > 0) {
    sq_key.keys = sq_key_allKeys[sq_key_allKeys.length - 1].name;
    sq_key.duration = sq_key_allKeys[sq_key_allKeys.length - 1].duration;
    sq.width = (10 * (t * 5));

if (sq_key.duration > 0) {
    psychoJS.experiment.addData('Square_Size', sq.width);
    continueRoutine = false;

I think most participants will answer fairly quickly, but I am fearful of those who get distracted for a few seconds and end up having a square much bigger than the expected (100px) causing us to believe that our manipulation was really successful when it was just a daydreaming participant!

Hello David,

thanks a lot for posting. As a matter of fact I always use PsychoPy auto-translate for the JS-part. So, I can’t help you with this. Do you have the Python-part? Or is your problem that the routine runs locally but not on-line?

Best wishes Jens

1 Like

Following ^^

Can you clarify exactly what behaviour you want the rectangle to be exhibiting? From your description, the circumstances under which it should/should not be increasing in width are not clear to me. For instance, should it depend on RT only? RT and also keypress duration? Just keypress duration, but not RT? (I feel like this should be relatively easy to achieve, but that may simply be because I don’t understand exactly what “this” is!)

Of course, apologies for not explaining it better!

The behavior I have:

  • Square initializes as a 10px x 100px rectangle
  • When the space key is pressed, the rectangle ‘grows’ by a factor of 10px * (t*5px)
  • Assuming all participants react within the first second or so, there is no issue
  • If participants are delayed in their response, the rectangle will start growing at a factor of 10px *(*however many seconds it has been since the trial began* * 5)
  • This can become an issue as if t = 3 there will be a pixel growth starting at (3*5,4*5,5*5...etc.)

The behavior I want:

  • Square initializes as a 10px x 100px rectangle
  • When the space key is pressed, the rectangle ‘grows’ by a factor of 10px * (t*5px)
  • In which t represents the start of the key press as opposed to the start of the presentation of the rectangle

It is probably very easy to achieve I am just not that well versed in the clock components!

Thank you for your time!

I see no definition of the t variable in begin exp section?
does this work at all?

Hi @Yogi_Amalantra,

I am by no means an expert but I thought that t was a constant, I have made a segmented demo that might make it clear what I am looking for.

This is the timeline:

Square moves left and right > Square growing task (10*(t*5))> fixation > (10*t)

I don’t have a license so this link should be active for about an hour or so!

Hello David,

you could either make your repository public for the time testing or post the psyexp-file to the forum (incl. other files that you need to get the toy-example to run). The pilot-links are only valid for an hour. So they are not a great help.

Best wishes Jens


Thanks for the clarification. If I’m understanding this right, then the width of the rectangle should be 10*((t-t_r)*5), where t is the current time, and t_r is the time at which the response was made. Is that correct? If so, then assuming that t_r is saved in sq_key.rt, all you should need to do is subtract this value from t in your “Each Frame” code, such that

sq.width = (10*((t-sq_key.rt)*5))

Edit: As an addendum to this, if you want the width to start at 10 pixels and grow from there, perhaps what you really want is 10 + 5(t-t_r), rather than 10 * 5(t-t_r). The former uses 10 as the baseline width and grows from there, whereas the width in the latter case will be 0 upon the initial key-press (because when the key is pressed, t = t_r), and increase as further time passes. But you would know this better than do I!

Thank you for the advice, I have made the repository public!

Currently the experiment will not run natively in PsychoPy as there are no elements with any Python code attached.

I have added your suggestions to my example, it seems the first method: sq.width = (10 * 5*(t-t_r))) works the best.

I see the logic in the example with the addition (sq.width = (10 + (5 * (t-t_r)));), but when I try it out it is more incremental than ‘constant’ as in the example with the multiplication but I could be making an error somewhere.

I realize the inability to run the experiment in the browser without a license is a bit of a hindrance here, if the repo does not work please let me know and I again thank you for your help!

Hello David

thanks for making it public. My knowledge of JS is very limited. I always use the auto-translated function of PsychoPy to generate of JS and try to correct the few things that auto-translation misses. So, I am afraid that I can’t help you here.

Sorry Jens

Cool, well if that code seems to do what you want it to, then that’s great. For the record though, 10 * 5 * (t-t_r) is equivalent to 0 + 50*(t-t_r), so if you want similar growth over time without the possibility of the square disappearing in the first frame, you should just need to make it 10 + 50*(t-t_r).

You are a lifesaver! I could not figure out why it was starting at 0px!