| Reference | Downloads | Github

Get access to current trial number

There are plenty of questions and hints online about this kind of requirement, probably most relevantly here, but I still can’t get it to work, suggesting that I’m missing something simple.

In the experiment builder, I’,m editing the .js in the Begin Routine section. I would like to set some parameters each routine, conditioned on the trial number. I therefore need the trial number, but I just can’t get hold of it.

Here is the function declaration and the first line

function Stim_PresentationRoutineBegin(snapshot) {
  return async function () {

Here is the list of things I have tried so far


and then reporting the output with console.log()
Trying getSnapshot() declares that the function does not exist. For the other options I always get undefined, except for the final option where ‘snapshot’ is NaN.

I’m not clear what my mistake is then. Do I need to do something fancy to get the current trial number?

What version of PsychoPy are you using? trials.thisN works in the newest release but did not work in earlier releases. So it might be that you need an update.


Hmm, okay

This is running using PsychoJS 2021.2.0 . I’ll try and find the newer version and test with that.

yes that will be the problem then. the release downloadable here will have trials.thisN


Hi Becca,

Thanks for this - this is certaintly a more elegant method that what I was using in the Python script, but it still doesn’t work for the onliny experiments in PsychoJS 2021.2.0. I have looked for the newer JS release but it won’t let me select one on Pavlovia.

Is there another way of doing this in Javascript?



Big oops from me, using trials.thisN IS actually working. I looked back at a different version of my code from yesterday when it wasn’t working and it seems I was accidentally overwriting it earlier in the script when I forgot to remove that previous attempt to access the trial number.