psychopy.org | Reference | Downloads | Github

trials.thisTrialN does not get correct trial number in javascript

Hi All,
I intended to add “take a break” within the trials loop. And the below python code works fine locally.

if not (trials.thisTrialN in [5,10]):
    continueRoutine = False

However, when I run it online using the following code:

alert(trials.thisTrialN)
if (trials.thisTrialN != 5 || trials.thisTrialN != 10){
    continueRoutine = false; 
}

The “take a break” message appears on every trial. And I found trials.thisTrialN says 0 all the time.
My question is how to get the current trial number in Javascript? Or, one step further, how can I do break within the experimental loop with the online experiment.

I read several topics related to this question but did not find a proper solution therefore I ask the question here again.

Thanks in advance.

trials.thisTrialN is always going to be not 5 or not 10. You need

alert(trials.thisTrialN)
if (trials.thisTrialN != 5 && trials.thisTrialN != 10){
    continueRoutine = false; 
}

(sorry this will stop on every trial apart from trials 5 and 10)

Right! || shouldn’t be the issue. I found this issue is due to trials.thisTrialN doesn’t get the correct trail numbers. Interestingly aleart(trials.thisIndex seems to say the correct index number but not thisTrailN.

I use trials.thisN not trials.thisTrialN

Please could you try with that?

I ran into the same issue and I tried it using both thisN and thisTrialN as the attributes.

thisTrialN prints 0 and thisN reports the total number of trials in the condition file. The numbers are reported correctly when tested locally using Python code.

Weirdly enough when I started a brand new experiment the trial number was reported correctly but not on my main experiment.

I had to add a variable which incremented at the end of the routine to fix this. However, this shouldn’t be “solution” so I actually also want to know the reason for this issue.

Hi @wakecarter
Thanks for your reply. In Javascript, alert(trials.thisN) gives me the total number of trials in the current loop. However, locally, in python both trials.thisN nad trials.thisTrialN showed the current trial number.

Hi @Tamer_Gezici

Thanks for sharing. I think, for now, this could be a ‘solution’.

Best,
Miao

You are welcome! Make sure the variable is initialized at 0 and is incremented at the end of the routine, I wrote it incorrectly there.

Hi @Tamer_Gezici,
Did you define the trail number as a global variable? If I define a trail number at Begin Experiments, javascript could not find the variable when increment because the variable is defined under the function function experimentInit(). Could you share this part of your code? Many thanks.

Miao

I did it by initialising the variable at the “Begin experiment” tab.

In Begin Experiments, I put

var mytrailN = 0;

In End Routine:

mytrailN++;
console.log(mytrailN);

I got the error mytrailN is not defined. Any idea why?

Miao

That should be correct unless you have misspelled the variable. I say that since you spelled it as “myTrail” instead of “myTrial”.

If that is not the case, clear browser cache and try again. If you use chrome do SHIFT+CTRL+J in the experiment page and click the “Sources tab” there you will see a .js file with the same name as your experiment. Right click it and open it in a new tab and search if you have the latest version online via CTRL+F’ing your variable in the page.

Thanks. I got a typo, but the variable names are constant in the code. I checked the .js file and it is the latest version. Unfortunately, still doesn’t work on my side.

Have you been able to solve it? Or shall I test this myself and see if I can replicate the issue?

Hi @Tamer_Gezici,

I found a very tricky part of the JS logical judgment.

I used

if (window.MYTRIALN > 60 || window.MYTRIALN < 60){
    //alert('insideloop');
    continueRoutine = false;
}

And put it under Each Frames. The code works.

However, when I switched to

if (window.MYTRIALN != 60){
    //alert('insideloop');
    continueRoutine = false;
}

The code stopped working. Lol

I was also inlighted by JavaScript - continueRoutine = false -- problem online

Therefore, I put it under each frame. Also, I initialized window.MYTRIALN as a global variable.

In sum, sort of working. Thank you so much.

Miao

That is a good idea but it may cause bugs. I am new to Psychopy so take my words with a grain of salt though.

You asked me for code earlier however I dont have that experiment anymore (I deleted it and started from scratch) here is an example experiment which you can take a look at its code. It shouldnt make a difference between using var_trial ++; and what I did but since I noticed the python code wasnt converted this way I did it using this way. See if doing it like this works. The variable I used is var_trial.

1 Like

@Miao I ran into similar errors and seems like I read many of the same threads you did. I decided to create my own counters and couldn’t get it to work until I copied the code into the “Each Frame” tab. I wrote in python in Builder and let PsychoPy produce the JS code and then I substituted my own counter variable. This is the setup I am using in my experiment for my break screen:

Begin Experiment:

exp_target_loop = 0;

Begin Routine:

if ((exp_target_loop !== 31)) {
    continueRoutine = false;
}

Each Frame:

if ((exp_target_loop !== 31)) {
    continueRoutine = false;
}

End Routine:

exp_target_loop += 1;

@Miao

I have found the solution, refer to my last post.

Cool Thank you for spending time on it!