| Reference | Downloads | Github

Loop.finished=true No longer working

thank you very much I appreciate it.

Best wishes,

Hello Alain,

Hope all is well. Is now a better time to have a look at my project?

Best Wishes,

Hello @TomH,

With my apologies for the delay (busy days!), these are the edits I would suggest (marked with // AP):

  • in trialRoutineBegin:
if ((Object.keys(obs_dict).length === 0)) { // AP
    // if ((obs_dict === {})) {
        for (let _pj_c = 0; _pj_c < trials.nStim; ++_pj_c) { // AP
        // for (var image, _pj_c = 0, _pj_a = trials.trialList, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
        //     image = _pj_a[_pj_c];
          const trial = trials.getTrial(_pj_c); // AP
          const image = trial["object"]; // AP
          obs_dict[image] = 1;
if ((obs_dict[image] === 2)) {
        if (! _pj.in_es6(0, Object.values(obs_dict)) && (! _pj.in_es6(1, Object.values(obs_dict)))) { // AP
        // if (((! _pj.in_es6(0, obs_dict.values())) && (! _pj.in_es6(1, obs_dict.values())))) {
            trials.finished = true;
            skipThisTrial = true;
            continueRoutine = false;
    } else {
        skipThisTrial = false;

With those modifications, your code runs on my end. I cannot vouch for its operational correctness, but it does run.
Have a go and let me know how you get on!



1 Like

Hi Alain,

Thanks for taking the time.

The loop is not behaving as it is supposed to online and is behaving very strangely.

To remind you of the way the loop is supposed to work:
(1) object is correct if within 150 pixels (i.e., inside the polygon i’ve included for easy testing)
(2) if an object is answered correctly twice in a row , (i.e., on two consecutive occasions that item, e.g., cow is attempted) it drops from the loop (i.e., its value goes from 0>1>2; if it is answered incorrectly, its value in the dict drops to 0 - the values for the other objects should stay the same here and they should still be skipped if their value reaches 2)
(3) loop terminates when all objects placed in correct location twice in a row (i.e., all objects values are 2 in dict).

EDIT: The java script is behaving in the following undesirable way:
(1) if an object is answered incorrectly it seems to reset to 0 as desired
(2) it will then finish all the objects in the that round of the loop
(3) it will then demand two new full loops in which I answer the original incorrect trial correctly (i.e., it is not skipping)
(4) then it will start a final round of the loop and terminate only once it reaches the object I originally answered incorrectly

note* currently I have all values of objects in dict set to 1 for quicker testing.
note* works fine in psychopy builder

do you know whats going wrong?


Hi Tom, I had a similar problem and posted my solution here: Cannot exit loop in online Pavlovia (Using JS code component)
Hope it helps!

Thanks Amy - it was useful to have a play around with the effect of putting certain code components in different windows (end routine,each frame etc) but it didn’t fix my problem, nor did the other growing list of threads on conditional loops in the forum. Not sure where to go next.


Hi David,

I’m surprised that this works (or perhaps it doesn’t any more) since I’m pretty sure currentLoop.thisN needs to be changed to trials.thisN

Please can you clarify when you can use the custom loop name and when you have to use trials?

Best wishes,


Hi @wakecarter, online you should always use trials when making reference to loop parameters, such as thisN or finished. This is because the trials argument for each function in the JS code is a snapshot of the current state of the trial handler, e.g., current trial number etc. It was originally set up so that currentLoop should make reference to the trial handler, as in the code you will see currentLoop = <loop name>, but this will not work because you need the snapshot instead. The generated code could be changed in future so that currentLoop points to the trials snapshot for each trial.

1 Like

Ah. I took currentLoop to be an example of a custom loop name rather than something with a specific meaning.

Hi @dvbridges

I know that this topic is heavily discussed in recent months due to PsychoPy versions however none of the solutions worked for me.

PsychoPy version = v2020.2.5

What I want to do: Participants should not perform the cate_name1_last loop at all if they do not make any mistake in the previous loop (cate_name1). If they do mistakes the mistaken trial To do a mistakeless first loop minimum of 20 trials are needed

Simply they see images and they need to choose one of the two buttons to respond. If they make a mistake, the mistaken image is added to an empty list. The list that is created by only mistaken trials need to be shown in loop cate_name1_last however if they do not make any mistakes in the previous loop experiment crushes because the list is empty. So I have the following code in the Routine named rountine_1_end

if len(learning1) == 0:
    cate_name1_last.finished = True
    cate_name1_last.finished = False


if ((learning1.length === 0)) {
    cate_name1_last.finished = true;
} else {
    cate_name1_last.finished = false;

However, I get no error message, and although the list learning1 has no item loop is not terminated.
Whenever I use

> trials.finished=true

I get the following error: error

I am a bit confused about what I need to do at this point.

Many thanks

I tried the same solution and got the same error. Any suggestions would be greatly appreciated.
changed JS code only. My loop is called trialsloop.


if ((key_resp_YorN.keys === “n”)) {
trialsloop.finished = true;
if ((key_resp_YorN.keys === “N”)) {
trialsloop.finished = true;


if ((key_resp_YorN.keys === “n”)) {
trials.finished = true;
if ((key_resp_YorN.keys === “N”)) {
trials.finished = true;

Hi @philBartlett,

Try this instead:
snapshot.finished = true;

Best, Thomas

Thanks Thomas. Gave it a try and got the following.


Could you try it again but generating code for the most recent version of PsychoPy?

Hi Thomas,

Do you want me to retry using snapshot and then provide you with the javascript code that is generated? I am not sure what you are asking for.


You are using 2020.2.5 which is before snapshot was added. You could update to the latest version, then try it.

I am using 2020.2.10. I just tried it again and got the same error. Is there something I need to do to declare snapshot?
The code I have written is in the “end routine” tab, should it be “begin routine”?

I haven’t tried snapshot myself yet. Try deactivating the study and then reactivating it.

I always change it from piloting to inactive then back to piloting every time I make a change. I just tried it again and got the same error.

I have put in a work around. Not very elegant but it does the job!
I initialize a variable practiceagain = 1 before the loop
If the exit loop condition is met I set a practiceagain = 0.
I put an inner loop around all the routines and set nReps to practiceagain.
That way it goes around the outer loop the remaining of the nReps but does nothing if practice is complete.

1 Like