# Get access to current trial number

**URL:** <https://discourse.psychopy.org/t/get-access-to-current-trial-number/24752>\
**Category:** Online experiments\
**Created:** [August 30, 2021, 5:35pm UTC](https://discourse.psychopy.org/t/get-access-to-current-trial-number/24752 "2021-08-30T17:35:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterVincent96](https://avatars.discourse-cdn.com/v4/letter/p/9fc348/32.png) [@PeterVincent96](https://discourse.psychopy.org/u/PeterVincent96)\
**Post date:** [August 30, 2021, 5:35pm UTC](https://discourse.psychopy.org/t/get-access-to-current-trial-number/24752/1 "2021-08-30T17:35:19Z")

</div>

There are plenty of questions and hints online about this kind of requirement, probably most relevantly [here](https://discourse.psychopy.org/t/trials-thistrialn-does-not-get-correct-trial-number-in-javascript/13860), 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

```auto
function Stim_PresentationRoutineBegin(snapshot) {
  return async function () {
    TrialHandler.fromSnapshot(snapshot);

```

Here is the list of things I have tried so far

```auto
trials.thisN;
trials.thisTrialN;
TrialHandler.thisN;
TrialHandler.thisTrialN;
TrialHandler.getSnapshot().thisTrialN;
snapshot.thisN;

```

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?

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [August 30, 2021, 9:05pm UTC](https://discourse.psychopy.org/t/get-access-to-current-trial-number/24752/2 "2021-08-30T21:05:05Z")

</div>

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.

Becca

---

<div class="post-metadata">

**Author:** ![PeterVincent96](https://avatars.discourse-cdn.com/v4/letter/p/9fc348/32.png) [@PeterVincent96](https://discourse.psychopy.org/u/PeterVincent96)\
**Post date:** [August 30, 2021, 9:19pm UTC](https://discourse.psychopy.org/t/get-access-to-current-trial-number/24752/3 "2021-08-30T21:19:23Z")

</div>

Hmm, okay

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

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [August 30, 2021, 9:48pm UTC](https://discourse.psychopy.org/t/get-access-to-current-trial-number/24752/4 "2021-08-30T21:48:54Z")

</div>

yes that will be the problem then. the release downloadable [here](https://www.psychopy.org/download.html) will have trials.thisN

Becca

---

<div class="post-metadata">

**Author:** ![PeterVincent96](https://avatars.discourse-cdn.com/v4/letter/p/9fc348/32.png) [@PeterVincent96](https://discourse.psychopy.org/u/PeterVincent96)\
**Post date:** [August 31, 2021, 9:41am UTC](https://discourse.psychopy.org/t/get-access-to-current-trial-number/24752/5 "2021-08-31T09:41:13Z")

</div>

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?

Thanks,

Peter

---

<div class="post-metadata">

**Author:** ![PeterVincent96](https://avatars.discourse-cdn.com/v4/letter/p/9fc348/32.png) [@PeterVincent96](https://discourse.psychopy.org/u/PeterVincent96)\
**Post date:** [August 31, 2021, 10:09am UTC](https://discourse.psychopy.org/t/get-access-to-current-trial-number/24752/6 "2021-08-31T10:09:36Z")

</div>

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.

Sorry!
