| Reference | Downloads | Github

Cannot exit loop in online Pavlovia (Using JS code component)

Description of the problem:

We tried to exit a loop ( the loop is named “RepeatTestLoop”) once our counter reaches a certain number (the counter is named “number_correct”). However, this does not work in the online Pavlovia version. The loop continues even after the counter is larger than 5.

In our code component, we did translate this into JS (as listed below)

if (number_correct > 5){ RepeatTestLoop.finished = true; }

else {number_correct = 0; }

Could someone let us know what the problem could be?

Best Regards,

1 Like

@katsuragiai, this will be fixed in the next release of PsychoPy (version 2020) - this issue and the solution has been reported here.

@dvbridges I have similar problem.

I’m creating an experiment ( with three training phases (eg. Routine:Telefon_Próba1_CIT) with feedback information (Telefon_Feedback_Proba1).

Each training phase has two loops - Inner (Sekw_tel_proba1), and outer (gdy_blad). Inner loop randomizes stimuli and outer loop is forcing repetition of each phase in the case of a wrong answer. I added code component in the end routine tab:

if k_resp_proba_1.corr == 0:

if ((k_resp_proba_1.corr === 0)) {
Sekw_tel_proba1.finished = true;
gdy_blad.finished = false;
} else {
gdy_blad.finished = true;

Everything works perfectly in PsychoPy, but it doesn’t work in the online Pavlovia version - errors don’t terminate an inner, and the correct answers don’t terminate an outer loop.
I have just downloaded Psychopy 2020 but it doesn’t help.

@Emiel_Regis, if you take a look at the link on GitHub, it shows the correct method for terminating loops using code.

@dvbridges Thank you for so quick response. Unfortunately, my problem is slightly different - I want to terminate each loop after all correct responses (outer loop) or after one wrong response (inner loop). I am a beginner at both PsychoPy and PsychoJS and I don’t know how to use your code in my experiment. I will be extremely grateful for a hint.

Hi! I was having a similar issue that I have solved (mostly). Hopefully this will be helpful to others.

I have a loop of trials, and wanted to randomly select 18 rows from a excel list of 216 rows. I tried entering $random(18)*215 into the “Selected Rows” box in the trial loop, which worked in the builder offine, but not in Pavlovia. I tried the recommendations in this thread and a couple of others: and Loop.finished=true No longer working which again worked in the builder, but neither worked online.

Then I found this old thread: My code component doesn't work in the online experiment, though works fine on desktop psychopy version

practrials is the name of my loop

I initialised a counter in the *Begin Experiment tab:
myCount = 0
that counts the trials in the *Begin Routine tab:
myCount = myCount + 1
Then in the *End Routine tab:

if myCount > 18:
    practrials.finished = True
    skipThisTrial = True
    continueRoutine = False 
    practrials.finished = False
    skipThisTrial = False
    continueRoutine = True

And finally in the *Each Frame tab:

if practrials.finished == True: 
    continueRoutine = False
    continueRoutine = True 

It is probably an inelegant solution, and some of it might be redundant. But it does exit the loop after 18 trials, running online on Pavlovia. One remaining issue is that in my datafile it still includes all 216 rows of my conditions file, so if anyone has any ideas how to fix that please let me know.

I’m new to these forums so apologies if this is unclear.

I am trying to end a loop after 5 trials have passed, and if there were at least 4 consecutive correct responses. I tried all of the solutions on the github page, but none of them worked to end the loop (trials.finished = true;, currentLoop.finished = true;). In a code component at ‘Begin routine’ I inserted:

//cumul is the cumulative correct answers, nTrials is the counter 
if (((nTrials > 5) && (cumul >= 4))) {
    msg = 'end';
    col = 'white';
    trials.finished = true;

The code works to break the loops in desktop Psychopy, but not online.

Thank you so much for your help in advance!

Put it in the “Each Frame” tab.

1 Like

Hi! I have a similar problem. The loop is just hanging after the 6 trials I wanted (ONLINE). Did it work for you?

Thank you @phoenix for your response. I solved my problem, but the solution was not in where the code was located. I tried putting the trials.finished = true; in the Each frame, but it still didn’t break the loop after the correct number of responses. @Kath_K I had two nested loops in the builder (following an example), but then realised there was no need for the outer loop. so leaving just one loop with a large number of repetitions, and adding trials.finished = true; in the Begin routine was successful.

Thank you for your fast reply! I still do not understand. Could you be so kind as to make a screen of your loop and code? I will really appreciate your kindness. I have been trying this for days on and no success.

What do you mean large number of repetitions?

many thanks again!

You should be able to download the code for my experiment if you go to Pavlovia > Explore > type ‘Practice’ and look for PhiliastidesLab’s experiment.

My builder looks like this: Screenshot 2020-04-27 at 16.57.00
My excel file contains 7 stimuli (faces). The loop ‘single_faces_learn’ loads this excel file and randomises the faces. Participants have to make a choice on a rating scale. If they have 4 consecutive choices that are correct, the loop (single_faces_learn) should finish.

My code component is under ‘feedback routine’ and records two variables: correct (corr) and cumulative (cumul). On each trial, if there is a correct answer corr = 1, if the answer is wrong corr = 0. ‘cumul’ only increases if there are consecutive correct answers. When there is a wrong answer ‘cumul’ is reset to 0. The JS code looks like this (in Begin routine):

if ((slider.getRating() === value_f)) {
    corr = 1;
    psychoJS.experiment.addData("corr", 1);
} else {
    corr = 0;
    psychoJS.experiment.addData("corr", 0)

if ((corr === 0)) {
    cumul = 0;
} else {
    cumul = (cumul + corr);

psychoJS.experiment.addData("cumul", cumul);

if ((cumul >= 4)) {
    trials.finished = true;

So fi the response on the slider is correct, that is slider.getRating() equals my pre-defined correct value (value_f), corr = 1.
And of course, each variable has to be defined in ‘Begin experiment’ section of the code component so:

corr = 0;
cumul = 0;

If your experiment is on Pavlovia, please share.

*By large number of repeats I mean 50. So the loop won’t stop repeating until participants have 4 consecutive correct answers.

Screenshot 2020-04-27 at 17.16.25

1 Like

Thanks a lot!

I am setting myCount1 = 0 in Begin experiment tab, in BeginRoutine tab I have

myCount 1 = myCount1+1
if (myCount1 > 5):
loop2.finished = True

it runs (offline) once and in the next repetitions it just “thinks” its the same counter and shows only 1 stimulus

Yes, your code didn’t work for me either. Try changing the first line to myCount1 += 1;, and the last line to trials.finished = true; It won’t work if you use the name of your loop. It should be the word ‘trials’.

So JS would be:

myCount1 += 1;
if ((myCount1 >= 5)) {
    trials.finished = true;

myCount1 += 1;

oh wow, what does it mean?
Unfortunately, this did not help. The loop gets stuck, black screen