# Exit a loop on pavlovia

**URL:** https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787
**Category:** Online experiments
**Created:** [April 27, 2020, 1:15pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787 "2020-04-27T13:15:22Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Kath\_K](https://avatars.discourse-cdn.com/v4/letter/k/6f9a4e/32.png) [@Kath\_K](https://discourse.psychopy.org/u/Kath_K)
#### Post date: [April 27, 2020, 1:15pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/1 "2020-04-27T13:15:22Z")

</div>

Hi!

I initially posted my question on [Pavlovia stuck in ititializing](https://discourse.psychopy.org/t/pavlovia-stuck-in-ititializing/12739)

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 ☹

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [April 27, 2020, 2:31pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/2 "2020-04-27T14:31:18Z")

</div>

Have a look through this whole thread: [Loop.finished=true No longer working](https://discourse.psychopy.org/t/loop-finished-true-no-longer-working/11190/3)

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:

```auto
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](https://discourse.psychopy.org/t/loop-finished-not-working/12273/2)

---

<div class="post-metadata">

### Author: ![Kath\_K](https://avatars.discourse-cdn.com/v4/letter/k/6f9a4e/32.png) [@Kath\_K](https://discourse.psychopy.org/u/Kath_K)
#### Post date: [April 27, 2020, 4:09pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/3 "2020-04-27T16:09:35Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [April 27, 2020, 4:22pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/4 "2020-04-27T16:22:49Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Kath\_K](https://avatars.discourse-cdn.com/v4/letter/k/6f9a4e/32.png) [@Kath\_K](https://discourse.psychopy.org/u/Kath_K)
#### Post date: [April 27, 2020, 4:27pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/5 "2020-04-27T16:27:04Z")

</div>

> [@Kath\_K](#):
>
> myCount1 = 0

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)

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [April 27, 2020, 4:39pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/6 "2020-04-27T16:39:20Z")

</div>

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:

```auto
myCount1 = 0

```

in end routine:

```auto
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.

---

<div class="post-metadata">

### Author: ![Kath\_K](https://avatars.discourse-cdn.com/v4/letter/k/6f9a4e/32.png) [@Kath\_K](https://discourse.psychopy.org/u/Kath_K)
#### Post date: [April 27, 2020, 4:43pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/7 "2020-04-27T16:43:50Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [April 27, 2020, 4:45pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/8 "2020-04-27T16:45:25Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Kath\_K](https://avatars.discourse-cdn.com/v4/letter/k/6f9a4e/32.png) [@Kath\_K](https://discourse.psychopy.org/u/Kath_K)
#### Post date: [April 27, 2020, 4:46pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/9 "2020-04-27T16:46:54Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [April 27, 2020, 5:00pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/10 "2020-04-27T17:00:31Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Kath\_K](https://avatars.discourse-cdn.com/v4/letter/k/6f9a4e/32.png) [@Kath\_K](https://discourse.psychopy.org/u/Kath_K)
#### Post date: [April 27, 2020, 5:14pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/11 "2020-04-27T17:14:10Z")

</div>

Thank you ever so much!

> **[Katharina Kühne / Sentence\_new](https://gitlab.pavlovia.org/Katharina_Kuehne/sentence_new)**
>
> Sign In/Register

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;  
}

---

<div class="post-metadata">

### Author: ![Katharina\_Kuhne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/katharina_kuhne/32/7546_2.png) [@Katharina\_Kuhne](https://discourse.psychopy.org/u/Katharina_Kuhne)
#### Post date: [April 27, 2020, 5:17pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/12 "2020-04-27T17:17:31Z")

</div>

my loop looks like this

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/3/5/356416199e11e2769e470f90515ef867e70115a0.png)

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.

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [April 27, 2020, 5:40pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/13 "2020-04-27T17:40:03Z")

</div>

> [@Kath\_K](#):
>
> I noticed that you added myCount1 = 0; again, maybe this is ma problem?
> 
> ```auto
> if (myCount1 > 5){
> console.log(“Setting trials.finished to true”);
> myCount1 = 0;
> trials.finished = true;
> }
> 
> ```

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:

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

```

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

---

<div class="post-metadata">

### Author: ![Kath\_K](https://avatars.discourse-cdn.com/v4/letter/k/6f9a4e/32.png) [@Kath\_K](https://discourse.psychopy.org/u/Kath_K)
#### Post date: [April 27, 2020, 5:55pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/14 "2020-04-27T17:55:52Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [April 27, 2020, 6:08pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/15 "2020-04-27T18:08:43Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Kath\_K](https://avatars.discourse-cdn.com/v4/letter/k/6f9a4e/32.png) [@Kath\_K](https://discourse.psychopy.org/u/Kath_K)
#### Post date: [April 27, 2020, 6:14pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/16 "2020-04-27T18:14:04Z")

</div>

![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/d/d/dd5dcd5e042e0b7be6327a3cd41202b203e68662.png)

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [April 27, 2020, 6:18pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/17 "2020-04-27T18:18:25Z")

</div>

Interesting. It copied the quotation marks incorrectly.

In PsychoPy in the code component that has this line:

```auto
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.

---

<div class="post-metadata">

### Author: ![Kath\_K](https://avatars.discourse-cdn.com/v4/letter/k/6f9a4e/32.png) [@Kath\_K](https://discourse.psychopy.org/u/Kath_K)
#### Post date: [April 27, 2020, 6:25pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/18 "2020-04-27T18:25:59Z")

</div>

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

> **[Katharina Kühne / TestDE](https://gitlab.pavlovia.org/Katharina_Kuehne/testde)**
>
> Sign In/Register

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [April 27, 2020, 6:27pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/19 "2020-04-27T18:27:24Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Kath\_K](https://avatars.discourse-cdn.com/v4/letter/k/6f9a4e/32.png) [@Kath\_K](https://discourse.psychopy.org/u/Kath_K)
#### Post date: [April 27, 2020, 6:35pm UTC](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787/20 "2020-04-27T18:35:31Z")

</div>

it’s all in red 😃 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

[Next page](https://discourse.psychopy.org/t/exit-a-loop-on-pavlovia/12787.md?page=2)
