psychopy.org | Reference | Downloads | Github

How to exit loop after certain number of trials with JavaScript?


#1

I’m using the builder to build an online experiment (running psychopy3 version 3.0.2 on a mac). I have 200+ trials set up in a loop and want to have the loop end after a certain number of random trials (e.g., 5). I’ve figured out that I can use the code snippets to do this and I got it to run correctly on my computer, but after reading a bit on here I realize it would have to be JavaScript for it to work online.

Can anyone tell me what the JS version of these python code snippets would be? I have a feeling this is probably pretty basic, but I don’t have any prior experience with Python or JS…

I have a snippet in the “begin experiment” section with:

myCount = 0

And I put this snippet in the “begin routine” section to exit the loop after 5 trial runs:

myCount = myCount + 1
if myCount > 5:
    trials_1.finished = True
    break

Please let me know if I can clarify anything. Thanks in advance.


#2

Yes, go to your Code Component and select “Both” so you can insert JS and Python alongside each other. We’ll hopefully have some rudimentary auto-translation python->JS eventually.

In your case, I think the correct JS code would be some line-ending semi-colons:

myCount = 0;

curly brackets for your if statement and True becomes lower case:

myCount = myCount + 1;
if (myCount > 5) {
    trials_1.finished = true;
    break;
}

In Chrome and Firefox (and presumably others) you can then debug and tweak the Javascript using “developer tools”.

@dvbridges and many others have better JS coding skills than me though so they may spot errors in the above


#3

Thanks for the reply, Jon. Unfortunately, that code doesn’t seem to do the trick. I googled a bit more in search of a solution and noticed that most JS scripts have something like:

var myCount = 0;

So as a general question: is it necessary to write var before defining a variable in psychopy? I tried it and it still didn’t work, so that can’t be the only issue, but I’m wondering if that’s something that takes place behind the scenes in psychopy or something that we’d need to remember to write in the JS side of the Code Component console.

Anyhow, do you (or anyone else) have further ideas for troubleshooting? Any suggestions would be greatly appreciated!

PS – I used safari’s developer tools and looked at the JS console, but didn’t see any error messages. I’m not exactly sure what I should be looking for. There were a lot of warnings, but they don’t seem related to the problem:


#4

@durkeepk, the fix to allow you to control when your loop ends is on its way.

Regarding defining variables in your code components, you do not need to define the variable with var, because PsychoPy will find and define them outside of your functions to give them global scope to be used across your experiment. If you are not seeing the variables defined as var above your functions in the JS code that PsychoPy outputs, then there must have been a problem with compiling the script.


#5

Thanks, @dvbridges and @jon for the help!

I just downloaded version 3.0.3 and I can see that the JS works now. I can display the myCount variable each frame both online and computer-based experiment. However, when I include the JS component that @jon recommended and try to run it online, the experiment doesn’t load—I only get a blank white screen (it works if I just run it locally). I looked at the JavaScript Console in Safari and there’s an error that says:

SyntaxError: 'break' is only valid inside a switch or loop statement.

I tried a few different things like specifying which loop to break (e.g., break trials_1;) but still can’t get it work for me. I’m not sure how to proceed from here. Do you have any ideas?


#6

I’ve only had to use ‘break’ to force an exit from a continuing loop (e.g. using ‘while’). I your case you may not need to use ‘break’ at all. Just a guess.