# Code conversion Jittered ITI in PsychoJS

Description of the problem: I’ve been trying to convert a jittered ITI routine from a PsychoPy study to PsychoJS but am having trouble with the code conversion.

The original code I used for python was:

jitter = np.arange(0.3, 0.6, 0.25)
shuffle(jitter)

This worked perfectly when run in PsychoPy. However, when running in Pavlovia, the jitter function is not defined. Is there a way to make this work in its current state? I looked into converting the code to Java and ended up with this instead:

jitter = Math.random()*(0.3 - 0.6) + 0.3;

I’m not too familiar with Java so the above didn’t seem to work either! If I need to use Java to get around the first problem, I would prefer a set of defined increments of 25ms, randomly chosen, as in the python version but after looking around have not been able to find the solution myself!

Any help on this is greatly appreciated

As an update, I’ve now started using this JS code below:

jitter = (Math.random()*(0.600 - 0.300) + 0.300).toFixed(3)
jitter = Math.round(jitter, 1)

I can’t seem to get the jitter to range between the values i’m looking for (300 - 600ms). The third line gives me a data point which suggests there is either 0ms jitter or 1000ms jitter.

Can anyone help further with this?

Many thanks

Hi Ral,

I think you need to define randint in code_JS

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

Then you can use

jitter=(randint(0,301)+300)/1000

I think that your problem is that you are rounding to the nearest second.

Also, you should note that Math.round doesn’t do what you might expect in JS. You’d need to define it as the following if you want to have any decimal places.

round = function(num, n=0) {
return +(Math.round(num + ("e+" + n))  + ("e-" + n));
}

Best wishes,

Wakefield

1 Like

Hi Wakefield

I’ve tried as you suggested and now experiment is running perfectly!

Thank you so much for taking the time to help me on this.

Best regards

Ral

Hi @wakecarter, seeing as you have helped Ral previously, it would be great if you could help me too… I have also have a problem with code conversion for jittered ITI and thought it might be better to reply to this thread instead of starting a new one. Yet, I think my code is more complex than Ral’s, as I also want to log the jittered ITI for the fixation cross for each trial.

I’ve attached screenshots of my Builder, as well as the fixation cross component, and the code components for the jittering and for logging the jittered ITI.

So, my Psychopy/python code can run without any issues and the ITI is successfully jittered when I run Psychopy, jittering the ITI from the set [0.4, 0.6, 0.8, 1, 1.2, 1.4].

BUT my problem is that my Javascript code doesn’t successfully jitter the ITI – the programme seems to choose one random ITI (e.g. 1.2s) from my set of values and the same ITI is then used for all trials when I run the online experiment on Pavlovia.org.

I previously have never used Javascript before so all the code conversion/translation has been based on my Googling, so please let me know if you figure out what’s wrong!

You are using trials.thisN which doesn’t work online.

Hi Wakefield,

I replaced trials.thisN with snapshot.thisN (referring to this thread trials.thisN and trials.thisTrialN are constant in the online experiment - #13 by jzacks) and it works now! Thank you!

1 Like