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)
I’m creating an experiment (https://pavlovia.org/Emiel_Regis/gnf_auto) 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:
python:
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.
@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: https://github.com/psychopy/psychojs/issues/68# and Loop.finished=true No longer working which again worked in the builder, but neither worked online.
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:
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 @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.
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:
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):
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.
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’.