Description of the problem: Hi, I’m trying to insert a break routine within a loop after a given number of trials. I used a code that I have used for a previous experiment, it used to work online in that experiment, but in the current one it only works locally. The experiment runs but no break routines.
This is what I used:
Code in the previous loop, “Begin routine” tab
my_trial_number = 0; #to start the trial counter
Code snippet on top of the “Break” routine, “Each frame” tab
You aren’t incrementing your variable, so it stays at zero. More than that, you are actively re-setting it to zero on every iteration. I guess you could instead set it to -1 at the beginning of the experiment, and increment it on the “begin routine” tab.
With Python, you could of course just use the automatically generated trial counter, e.g. via your_loop_name.thisN. I’m not sure what the PsychoJS equivalent of that is.
Regardless, the if check should also be in the “begin routine” tab, rather than the “each frame” tab, as currently, you will actually be displaying at least one frame of the routine, regardless of the trial number.
Thanks @Michael for your reply.
I assumed the increment was automatic, especially since the exact same code works for another experiment (still today).
I copied the JS code to the “begin routine” tab but no luck.
In Python I do use trials.thisN and it works perfectly.
It’s just really strange that the JS code isn’t working for that experiment.
Your variable is called my_trial_number. So by definition, it is something you are creating yourself, and will be responsible for incrementing or manipulation in any other way. Builder will keep track of its own variables (like trials.thisN) but it can’t possibly know what to do with any custom variables you create. Computers only do what they are explicitly told to do.
It really isn’t strange. The code you have posted above can’t possibly work, because the trial number is not being incremented.
So you either need to use a trial counter that Builder maintains for you, or manually increment your custom one.
Okay I think I got it!
Your reply made me look closer and I am only now noticing that I had a little line that said my_trial_number +=1; so that must be it. I’m unable to test it at the moment because I have syncing problems, but that’s for a different post. Thank you.