| Reference | Downloads | Github

Outer loop terminates prematurely when setting inner loop to finished - continueRoutine issue?

URL of experiment:

Description of the problem: I am experiencing a problem where I have nested loops and wish to terminate the inner loop (only) given certain responses in the outer loop. However, the outer loop (or its routines) seems to also be cancelled inadvertently.

The experiment runs correctly using the python code but I encounter an error when using PsychoJS. Specifically, when I abort the inner loop I use:

continueRoutine = false

…in the ‘each frame’ code component, in order to prevent the remainder of the routine in the aborted loop from being conducted. However, as a side effect, this seems to either (1) also exit the outer loop prematurely, or (2) cancel the routines in the outer loop as well. The experiment simply ends when it should not because there are additional iterations in the outer loop remaining. Importantly, if I do not attempt to end the inner loop early, the outer loop completes all iterations without a problem.

The linked experiment above is a minimal reproducible example of the phenomenon. I wonder if the continueRoutine = false code is also affecting other routines, perhaps due to global scope? Is there a way to specify a specific routine to be discontinued?

I’ve been struggling with this for days and am very grateful for any insights!

Yes, setting continueRoutine=false on every frame will cause that routine never to run at all for any loop.

If you want to end a loop you need:


Hi @jon, many thanks for your reply. To clarify:

I do use:


to kill the loop.

I then also use:


in the ‘each frame’ code component for the routine within the (inner) loop being ended. This is done within an if statement so that continueRoutine is only set to false if the conditional statement that should end the loop is true. Here is the code for the each frame component of the inner loop routine:

if (skip_step_two) {
step2_loop.finished = true;
continueRoutine = false;

I do this because in another thread @dvbridges suggests that while stopping the loop works fine with PsychoJS, this will not end the remaining components of the routine within the loop unless continueRoutine is set to false each frame.

The unexpected consequence in my code is that the outer loop and / or its routines also seem to be aborted when setting the inner loop to finished. In other words, when setting the inner loop to finished, and the inner loop routine to continueRoutine = false, this seems to affect the outer loop and its routines. So, I am wondering if the continueRoutine = false code is also unintentionally affecting other routines in the outer loop (outside of the specific routine were continueRoutine was set to false).

Thanks so much if you or @dvbridges have additional thoughts!

continueRoutine = false; has no direct effect on the loops, inner or outer. What might be happening is tha teh value of skip_step_two is never returned to be false. If it’s always true then this routine ends itself every time it starts and therefore looks like the loop ended (it just ran really fast with no visible routines in it?)

Thanks again for your reply @jon. It’s good to know that continueRoutine = false; has no impact on the loops. My issue is not that inner loop or associated routines fail to execute but rather that the outer loop seems to end prematurely. Here is the overall flow:


If skip_step_two = false, then step 1 completes, followed by the step_2_loop before returning to the next iteration of the trials_loop (which is the outer loop). This is expected and runs correctly.

What is unexpected – and only occurs with PsychoJS (but not PsychoPy)– is that if skip_step_two = true, not only does step_2 not execute (which is expected) but the remaining iterations of the trials_loop, which contain step 1 also fail to execute (or the step1 routine is also suppressed, making it seem as if trials_loop has ended.

So, as soon as I implement skip_step_two = true, the entire experiment concludes when instead it should return to the next iteration of the trials_loop.

Do you have thoughts on what might cause this behavior? Thanks again for your helpfulness!

For any others reading this thread, it looks like the issue may be a bug which is being addressed here.