psychopy.org | Reference | Downloads | Github

Exit a loop on pavlovia

Hi!

I initially posted my question on Pavlovia stuck in ititializing

but since it seems to be a different issue I am reformulating it. Apologies if it is not allowed.

I am trying to exit one of my 3 loops on pavlovia after 6 trials.

In Begin routine (I also tried Begin Experiment though)

I have

myCount1 = 0

In End Routine (also tried every Frame)

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

In JS

I used trials instead of loop2

trials.finished = true;

Please help, since I need to start the experiment as urgent as possibly :frowning:

Have a look through this whole thread: Loop.finished=true No longer working

To distinguish exactly what the problem is, some logging might be helpful. Is it that trials.finished=true isn’t doing anything? Or is it that the condition isn’t being met?

I recommend the following for your JS code:

myCount1 = myCount1 + 1;
console.log(myCount1);
if (myCount1 > 5){
    console.log("Setting trials.finished to true");
    trials.finished = true;
}

These messages will appear in your browser’s JavaScript console, which you can find in Chrome by going to View -> Developer. That will tell you if MyCount1 is updating correctly, and if the condition is ever being met. The big thing to watch out for is if you see multiple instances of the message “Setting trials.finished to true”, because that would mean it’s failing to exit the loop correctly.

Looking back at the other thread you also have a lot of nested loops. Keep in mind that trials.finished will always apply to the most proximal loop to the trial this code is in. You may want to look at dummy trials if you are trying to get out of multiple layers of nested loops. See here: Loop.finished not working

1 Like

Thank you for your quick reply.
I am sorry for my maybe stupid question, but what is the corrrect tab to put the specific codes (setting to 0, counting and trials.finished part?) If I put the counter in Begin experiment tab even offline it goes through it only once (I need to repeat the loop 10 times with different sets of 6 stimuli from a big list).

If there were some random option for pavlovia to take 6 random items from the list, it would be perfect! The choice option worked well offline but not on pavlovia.

Many thanks again.

I would put that in the “end routine” tab of one of the trials in your loop.

The choice option not working on Pavlovia I don’t know much about. @jon does the JS trialhandler do choice at this time?

Thanks! What about myCount1 = 0
Apparantly it counts throughout the experiment, but I want to reset it every time I repeat the loop (10 times)

The simplest solution is probably to put it once in begin experiment, and then a second time in the conditional that ends the loop. So, in begin experiment:

myCount1 = 0

in end routine:

myCount1 = myCount1 + 1;
console.log(myCount1);
if (myCount1 > 5){
    console.log("Setting trials.finished to true");
    myCount1 = 0;
    trials.finished = true;
}

That way, every time you jump out of the loop it resets the counter for you, at least in theory.

The problem is it does not reset. Even offline. The first time it goes all right 6 trials and stop
and the next time it still things that the myCount is > 5 which is true if we continue counting, and only shows 1 trail etc

Keep in mind that the JS code and the Python code are different, and if you’re running locally you’re only seeing the Python code. Did you put myCount1 = 0 into the same conditional statement in your Python code?

I am now trying to have it at least offline. But even offline it does not work :frowning:

I’m afraid I need more information about what exactly you have set up. Can you link your code repository on pavlovia (go to “view code” and link the page there, and go to settings -> general -> permissions and set it to public) or upload your experiment here so I can have a look at it? I don’t have enough information right now to figure out why it might not be working.

Thank you ever so much!

I noticed that you added myCount1 = 0; again, maybe this is ma problem?

if (myCount1 > 5){
console.log(“Setting trials.finished to true”);
myCount1 = 0;
trials.finished = true;
}

my loop looks like this

I present a story (alternatively from one of two files), 6 sentences with feedback (again alternately from one of two files belonging to the story), and a math task.

The idea behind this was that it resets the counter when the condition is met. You want that in both your JS and your Python code to test my solution. So, in your Python code:

            if myCount1 > 5:
                loop2.finished = True 
                myCount = 0

You want this particular block of code at “end routine” in your “feedback” trial type.

Now it works fine offline but is immediately stuck in initializing on pavlovia :(((

Oh no this will never end

I appreciate your guidance very much, thousand thanks!

Progress is failing differently.

In your web browser, when it gets stuck on initializing, open up the JavaScript console. (In Chrome, you can find this by going to View -> Developer.) This should give you a more informative error message about why it’s getting stuck.

Interesting. It copied the quotation marks incorrectly.

In PsychoPy in the code component that has this line:

console.log("Setting trials.finished to true");

delete the quotation marks and replace them with single quotes. Don’t copy and paste from here, type them in. The issue is that it somehow filled the code component in with “educated” quotes that are different shape at the beginning and end rather than plain quotes, and JS doesn’t know how to read educated quotes.

ok, now it starts but gets stuck (black screen) after trial 6

In the console there is huge list of errors, dont know I should look for

I actually saved it in a new project since updating was freezing

Anything in red, and anything that references “cotidie_de1_withcode.js” and gives a line number, ideally, but whatever you can share may be helpful. That said, it sounds like it is successfully breaking after six trials.

it’s all in red :smiley: three pages

just copied some

6
cotidie_de1_withcode.js:2684 Setting trials.finished to true
core-2020.1.js:1437 TypeError: Object.keys is not a function
at ExperimentHandler.isEntryEmpty (data-2020.1.js:33)
at Scheduler._currentTask (cotidie_de1_withcode.js:3007)
at Scheduler._runNextTasks (util-2020.1.js:1091)
at Scheduler._runNextTasks (util-2020.1.js:1094)
at Scheduler._runNextTasks (util-2020.1.js:1094)
at Scheduler._runNextTasks (util-2020.1.js:1094)
at Scheduler._runNextTasks (util-2020.1.js:1094)
at update (util-2020.1.js:1058)
window.onerror @ core-2020.1.js:1437
data-2020.1.js:33 Uncaught TypeError: Object.keys is not a function
at ExperimentHandler.isEntryEmpty (data-2020.1.js:33)
at Scheduler._currentTask (cotidie_de1_withcode.js:3007)
at Scheduler._runNextTasks (util-2020.1.js:1091)
at Scheduler._runNextTasks (util-2020.1.js:1094)
at Scheduler._runNextTasks (util-2020.1.js:1094)
at Scheduler._runNextTasks (util-2020.1.js:1094)
at Scheduler._runNextTasks (util-2020.1.js:1094)
at update (util-2020.1.js:1058)
jquery.min.js:2 Uncaught TypeError: Object is not a function
at Function.makeArray (jquery.min.js:2)
at new n.fn.init (jquery.min.js:2)
at n (jquery.min.js:2)
at t..._on (jquery-ui.min.js:6)
at t..._classes (jquery-ui.min.js:6)
at t..._toggleClass (jquery-ui.min.js:6)
at t..._removeClass (jquery-ui.min.js:6)
at n.fn.init. (jquery-ui.min.js:6)
at Function.each (jquery.min.js:2)
at t...destroy (jquery-ui.min.js:6)
core-2020.1.js:1437 null
window.onerror @ core-2020.1.js:1437
Tone.min.js:1 Uncaught TypeError: Cannot read property ‘toString’ of undefined
at Function.n.isObject (Tone.min.js:1)
at Function.n.defaultArg (Tone.min.js:1)
at s.default.TimelineState.s.default.Timeline._search (Tone.min.js:1)
at s.default.TimelineState.s.default.Timeline.forEachBetween (Tone.min.js:1)
at s.default.Clock._loop (Tone.min.js:1)
at s.default.Context.s.default.Emitter.emit (Tone.min.js:1)
jquery.min.js:2 Uncaught TypeError: Object is not a function
at Function.makeArray (jquery.min.js:2)
at new n.fn.init (jquery.min.js:2)
at n (jquery.min.js:2)
at t..._on (jquery-ui.min.js:6)
at t..._classes (jquery-ui.min.js:6)
at t..._toggleClass (jquery-ui.min.js:6)
at t..._removeClass (jquery-ui.min.js:6)
at n.fn.init. (jquery-ui.min.js:6)
at Function.each (jquery.min.js:2)
at t...destroy (jquery-ui.min.js:6)
core-2020.1.js:1437 null