psychopy.org | Reference | Downloads | Github

Manually defined functions don't work when running experiment online

URL of experiment: Sign in · GitLab

Description of the problem:

Hello there! I have a question concerning the code components when running an experiment online on Pavlovia. Due to a somewhat more complex experiment design I had to load images “manually” via a code component and store them in an array. Before each block/trial I need to shuffle the images. Running the experiment locally works perfectly fine with np.random.shuffle(array). For running it online I translated it into shuffleArray(array) and defined the function shuffleArray() at the beginning of the experiment. The definition of the function is exported into the js files, but when starting the experiment online, I still get the error that the function was not defined:

grafik

Is something wrong with the definition?
grafik

I tried both: inserting a JS Code Component at the beginning of the experiment (in the first routine) and inserting it in the “Begin Experiment” section of the Code Component in the Routine where I need the shuffleArray() function. Both did not work.

I think you need to double define your functions because they are not inmediately assigned to the name you geve them in JS. Have a look at this:

oh okay. I will try that. Thank you very much :slight_smile:

Yup, that fixed it. Thank you so much for your immediate help!

I think that all you need to do this is to prefix your function with name =, e.g.

randint = function(min, maxplusone) {
  return Math.floor(Math.random() * (maxplusone - min) ) + min;
}

1 Like

Yes, that was the problem. Makes totally sense :smiley: Thank you!