| Reference | Downloads | Github

"Take a break" works locally but not online

Description of the problem:
I tried to insert a break after every 100 trials by adding a routine. The following lines were added.

if not trialsLoop.thisTrialN in [100, 200, 300, 400, 500]:
continueRoutine = False

This works perfectly when PsychoPy runs off the local computer. But when tested online, the break is shown after every trial (instead of every 100 trials). I searched on old threads here - someone else had this problem before, but I did not see a solution. Why does JavaScript fail to take in this command?

1 Like

Please link your experiment and make sure it is public.

I assume this is a code component in builder, so make sure code type is set to “auto->JS”, but if the translation isn’t working, I’ll need to look at the JavaScript its producing to figure out why.

I am also struggling with this problem. I used a similar code that works locally but it did not online. My experiment is currently not online because I am still trying to get other parts of it to work, but attached is the code component in builder with the js translation that I get in my experiment. Hopefully that’s a helpful start.

Note that: trial_sentence is the name of my loop. I am inserting my breaks after every 20 trials.

The first image is the side-by-side codes

The second image is the js code only because it was cutoff in the first image.

Yeah the whole “in” thing in Python does not translate well to JavaScript, it has to create this whole helper function mess.

For bug-hunting, start by trying the much less elegant solution (for @jiang166’s code):

if (trial_sentence.thisTrialN == 100){
    continueRoutine = false;

and so on for each specific trial number. Or use the slightly more elegant:

if (trial_sentence.thisTrialN % 100 == 0){
    continueRoutine = false;

(% is a modulo operator, so this is saying if the trial number is an exact multiple of 100, do this)

For your code @phamther, I think you’re putting this in the break routine and canceling it except on these trials, so just use falsifications of the above conditionals (!= instead of ==)

Basically, let’s start by ruling out whether “in” is the problem and go from there. If it’s not the “in” operator, it’s probably going to be an issue with thisTrialsN, which I will have to dig deeper to figure out.

Thanks for your prompt response! I have actually made a truncated version of my experiment to focus on fixing this break issue. Link to the repository:

I am using a break routine so I tried both codes with the falsifications (!= instead of ==) but the break routine is now appearing after every trial. I was reading this post (Pause routine online) and I also tried to set the thisTrialsN counter to 0 but the break routine is still appearing after every trial (just trying any creative solutions, I have no experience with coding). Thanks again for your help!

Either the condition is never been met or continueRoutine isn’t sticking. I’ve seen some mentions that JS has trouble referencing the current loop by name. Let’s try this:

if (trials.thisTrialN % 20 > 0){
    continueRoutine = false;
    console.log('no break here')

If you open your javascript console (in Chrome it’s in the menu under view->developer), the log messages should appear there. This gives us the ability to test two things separately. If the condition is not being met, then it’ll just show you the break on every trial, but the console log will remain blank. If the problem is with continueRoutine, then it will show you the break on every trial, but it will also fill up the console log with these messages. That’ll help us narrow down the root issue.

1 Like

Thanks, this code works for the most part! Except that the break is also appearing before the first trial (if I put the break routine before the main routine) or after the first trial (if I put the break routine after the main routine). But after that, the break is appearing after every 20 trials.

There is no messages appearing in the console with this code.

Just a note, I also tried it with the name of my loop and it did not work. The break was appearing after every trial - confirming what you had mentioned, that JS has trouble referencing the current loop by name.

Ah, that’s an easy quirk to fix. The issue is that “before first trial” means the loop count is 0, and 0 % 20 (modulo 20) is 0. So, we can make a slightly more complex if statement to circumvent this:

if (trials.thisTrialN % 20 > 0 && trials.thisTrialsN > 0){
    continueRoutine = false;
    console.log('no break here')
1 Like

Unfortunately that break before the first trial is still happening.

I noticed that in “trials.thisTrialsN > 0”, thisTrialN should be singular so I fixed that in the code that I tried. I also tried it with parentheses in case that made a difference but to no avail

Try >1. There’s probably something simple I’m missing about either what thisTrialN is set to or that conditional, but it’s not obvious to me. It should be simple ish.

What if the break is at the trial number 199, 399, 599, 799 since trials start at 0?

Easily modifiable. trials.thisTrialN % 200 == 199

Thanks for your response. Replacing “if not in” with “if…” is in principle fine, but did not work in practice. After revising the code, it continues to run locally in PsychoPy, but not online. The “take a break” still happens after every trial online, suggesting the code was ignored in Javascript. I don’t know how to change the visibility of my project public (it’s in pilot mode). Here’s the Python to JS translation:

Just taking a stab at it but have you tried running it with the loop name as “trials” instead of “trialsLoop”. After some trial-and-error yesterday, I noticed that my code was working when I used “trials” but not when using the name of my loop

Thanks for your response. The names “trials” vs. “trialsLoop” are arbitrary; I happened to define the loop around my trials as “trialsLoop”. If I change it to “trials” and also update the codes, it does not affect the function of the script. It continues to run properly on local PsychoPy, but the “take a break” part of the code is not skipped according to the script, when run online.

Thanks Jonathan. When run online, the console shows the message “no break here” after every single trial.

That indicates that the condition is always being met. Looking at the last js code you posted, first of all it does need to be trials.thisTrialN, specifically (javascript has trouble referencing the current loop by name for reasons that are unclear, but the developers are working on it). Beyond that I’m not sure why it would be true on every trial.

Let’s try two things:

  1. Please post the javascript you are currently using that is generating this behavior.
  2. Let’s try adding a more informative log statement. Replace the console.log line with the following:

This will tell us exactly what the trial number is, or what it thinks it is, which will help us determine why the condition is not being met.

Here is the javascript (sorry for not including it previously; I only just figured out how to make it public).

The console log accurately incremented trials.thisTrialN - 0, 1, 2, and so on. The problem seems to be that the commands are ignored by continueRoutine.

Pavlovia is down right now so I’ll have to look at it later, but continueRoutine failing is weird. Is this an embedded loop within a loop situation?

The experiment structure is attached here as a figure. There is a loop (trials) that includes three components: SearchTrial (the task of the trial), feedback (correct or incorrect for that trial), and TakeABreak (this routine should be skipped on most trials. This one contains the continueRoutine = False). PsychoPy correctly interpreted the commands, but Javascript did not.

By the way, some problems I encountered in PsychoPy to JS seem intractable. Perhaps there’s a PsychoPy development issue here. From Builder to JS works well if there aren’t any customized codes inserted into Builder. When codes are inserted, the converted JS just doesn’t seem to handle the codes well - it can’t find variables, functions, etc.

Perhaps this is a major area of further development for PsychoPy.