| Reference | Downloads | Github

Help in translating a small Python snypet to JS for online experiment

Hi, I just realized that I need to change my custom code from python to JS in order to run my experiment properly in Pavlovia.

I have the following code in the “End Routine” tab of a trial loop (enclosed inside another loop)

if trials.thisN == 0: 
    trials.finished = True # end the loop early

How should I change it to JS? This should be really easy but I’m having a hard time since I don’t code in JS. Could someone give me a hand? Thanks

Hi @aendrs, it is not much different to Python, but at the moment, you are better off creating your own trial counter than using the trial handlers attributes. For example, open your code component and set code type to “Both”. In the relevant tabs in the right hand panel for JS:

// Begin experiment
counter = 0;

// Begin Routine
if (counter === 0)
  trials.finished = true;

// End Routine
counter += 1;

Hi @dvbridges, thanks for your kind reply.
Unfortunately I cannot make it work, I’m unable to replicate the behavior I had with the python code. The trial continues in a loop and doesn’t abort after the first random iteration. Do you have any idea how could I try another solution? Thanks in advance

@aendrs, which version of PsychoPy are you using?

I’m using version 3.1.2

Great. Can you please share your URL? I can take a look and see what is happening (but see the other post regarding ending loops).

Hi @dvbridges
In the end I, since I just wanted a single random loop, I solved it using the the inner part of the IF condition you provided me,

trials.finished = true;

it worked properly, thanks for your help.

However, if I may ask another question, my experiment is saving the data properly when run locally, but it is not saving the data online, it gives me empty .CSV files. I have tried to modify different parameters but no luck so far. Could you please point me in an appropriate direction? What do you suggest?

The URL of the experiment is :