psychopy.org | Reference | Downloads | Github

Need help in "translating" jiter into JS

Hello!

I have used a jitter function (?) for ITI ($current_jitter1) in python offline and it works perfectly but apparently is not correct in JS:

if loop_pract.thisN == 0: 
    jitters = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] * 2
    shuffle(jitters) 
current_jitter = jitters.pop() 
thisExp.addData('jitter', current_jitter)

AND on JS:

if ((loop_pract.thisN === 0)) {
    jitters = ([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] * 2);
    shuffle(jitters);
}
current_jitter = jitters.pop();
thisExp.addData("jitter", current_jitter);

$current_jitter1 serves as ITI duration.
I would be very grateful for your help!

Hi!
With the line below I guess you’re trying to have each element of the array appear twice right?

right :slight_smile:

Check! Yeah, that doesn’t work in JS I’m afraid. An easy fix is having each element appear twice, sans the *
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]

Thank you!
I’m afraud it’s not just about this. It gives me en error

  • TypeError: Cannot read property ‘pop’ of undefined

Ah, I have a memory that thisN doesn’t work certain versions of PsychoJS, but it should in more recent versions, see the crib sheet: https://docs.google.com/document/d/13jp0QAqQeFlYSjeZS0fDInvgaDzBXjGQNe4VNKbbNHQ/edit

What I’d do in your case first is add some debugging script so you can see what happens. I added some console.log statements to your original script. These produce messages in the browser console (see the Crib Sheet again).

if ((loop_pract.thisN === 0)) {
    console.log('generate jitters'); // Print that we're here now
    jitters = ([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] * 2);
    shuffle(jitters);
    console.log(jitters); // Print output of shuffle
}
// Print we're popping and print jitters again
console.log('pop jitters');
console.log(jitters);
current_jitter = jitters.pop();
thisExp.addData("jitter", current_jitter);

Thank you, I will try this out. However, I think the whole jiiter thing does not really work on JS.

To set some stimulus onset you mean?

I think this function does not exist in JS, at least there were some problems some years ago.
I get
Failed to load resource: the server responded with a status of 404 ()
log4javascript.min.js:1 INFO unknown | [PsychoJS] Start Experiment.
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: fix as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: gng_pract as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the image of ImageStim: gng_react with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: image in PsychObject: gng_react as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the mask of ImageStim: gng_react with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: mask in PsychObject: gng_react as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: text_feedback_pract as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the image of ImageStim: ITI_pract_2 with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: image in PsychObject: ITI_pract_2 as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the mask of ImageStim: ITI_pract_2 with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: mask in PsychObject: ITI_pract_2 as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: text_pract_end as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: fix_gng as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: gng_stimulus as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the image of ImageStim: gng_react_2 with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: image in PsychObject: gng_react_2 as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the mask of ImageStim: gng_react_2 with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: mask in PsychObject: gng_react_2 as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the image of ImageStim: ITI_pract with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: image in PsychObject: ITI_pract as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the mask of ImageStim: ITI_pract with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: mask in PsychObject: ITI_pract as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: ib_break as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: thanks_text as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: seed in PsychObject: TrialHandler as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
GNG_test1.js:425 pop jitters
GNG_test1.js:426 undefined
core-2020.1.js:1480 TypeError: Cannot read property ‘pop’ of undefined
at Scheduler._currentTask (GNG_test1.js:427)
at Scheduler._runNextTasks (util-2020.1.js:1093)
at Scheduler._runNextTasks (util-2020.1.js:1096)
at Scheduler._runNextTasks (util-2020.1.js:1096)
at update (util-2020.1.js:1060)
window.onerror @ core-2020.1.js:1480
log4javascript.min.js:1 FATAL unknown | {}
BrowserConsoleAppender.append @ log4javascript.min.js:1
core-2020.1.js:589 windowAlreadyInFullScreen: true

And many thanks for your guidance :slight_smile:

Look at my earlier reply; it might give you some pointers.

Hmmm
I am still not getting it :frowning:

This part of your console logs…

… is generated by this code …

console.log('pop jitters');
console.log(jitters);

That means jitters is undefined. So you can’t pop it, which triggers…

Thank you.
I have updated PsychPy but still get

  • Unspecified JavaScript error
if ((loop_pract.thisN === 0)) {
    console.log('generate jitters'); // Print that we're here now
    jitters = ([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]);
    shuffle(jitters);
    console.log(jitters); // Print output of shuffle
}
// Print we're popping and print jitters again
console.log('pop jitters');
console.log(jitters);
current_jitter = jitters.pop();
thisExp.addData("jitter", current_jitter);

I do not understand where the problem in defining it is (

What does the console say?

INFO unknown | [PsychoJS] Start Experiment.
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: fix as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: gng_pract as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the image of ImageStim: gng_react with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: image in PsychObject: gng_react as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the mask of ImageStim: gng_react with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: mask in PsychObject: gng_react as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: text_feedback_pract as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the image of ImageStim: ITI_pract_2 with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: image in PsychObject: ITI_pract_2 as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the mask of ImageStim: ITI_pract_2 with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: mask in PsychObject: ITI_pract_2 as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: text_pract_end as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: fix_gng as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: gng_stimulus as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the image of ImageStim: gng_react_2 with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: image in PsychObject: gng_react_2 as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the mask of ImageStim: gng_react_2 with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: mask in PsychObject: gng_react_2 as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the image of ImageStim: ITI_pract with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: image in PsychObject: ITI_pract as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the mask of ImageStim: ITI_pract with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: mask in PsychObject: ITI_pract as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: ib_break as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: thanks_text as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: seed in PsychObject: TrialHandler as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
GNG_test1.js:431 pop jitters
GNG_test1.js:432 undefined
core-2020.2.js:1822 TypeError: Cannot read property ‘pop’ of undefined
at Scheduler._currentTask (GNG_test1.js:433)
at Scheduler._runNextTasks (util-2020.2.js:1567)
at Scheduler._runNextTasks (util-2020.2.js:1571)
at Scheduler._runNextTasks (util-2020.2.js:1571)
at update (util-2020.2.js:1524)
window.onerror @ core-2020.2.js:1822
log4javascript.min.js:1 FATAL unknown | {}
BrowserConsoleAppender.append @ log4javascript.min.js:1
core-2020.2.js:721 windowAlreadyInFullScreen: true

I think it is still about defining the jiiters thing

Yep. Could be that jitters isn’t created or that it is created, but not available when you want to pop it. Note that in my additions to your code, I added console.log(‘generate jitters’), but I don’t see this message appear in the console. That makes me suspect that part of the code isn’t executed. Maybe add some more debugging, to see if the Boolean expression in your if statement acts as you would expect. For instance, log loop_pract.thisN

Thank you. I have added it and get

log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: fix as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: gng_pract as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the image of ImageStim: gng_react with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: image in PsychObject: gng_react as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the mask of ImageStim: gng_react with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: mask in PsychObject: gng_react as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: text_feedback_pract as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the image of ImageStim: ITI_pract_2 with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: image in PsychObject: ITI_pract_2 as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the mask of ImageStim: ITI_pract_2 with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: mask in PsychObject: ITI_pract_2 as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: text_pract_end as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: fix_gng as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: gng_stimulus as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the image of ImageStim: gng_react_2 with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: image in PsychObject: gng_react_2 as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the mask of ImageStim: gng_react_2 with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: mask in PsychObject: gng_react_2 as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the image of ImageStim: ITI_pract with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: image in PsychObject: ITI_pract as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the mask of ImageStim: ITI_pract with argument: undefined.
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: mask in PsychObject: ITI_pract as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: ib_break as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: size in PsychObject: thanks_text as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
log4javascript.min.js:1 WARN unknown | setting the value of attribute: seed in PsychObject: TrialHandler as: undefined
BrowserConsoleAppender.append @ log4javascript.min.js:1
GNG_test1.js:463 pop jitters
GNG_test1.js:464 undefined
core-2020.2.js:1822 TypeError: Cannot read property ‘pop’ of undefined
at Scheduler._currentTask (GNG_test1.js:465)
at Scheduler._runNextTasks (util-2020.2.js:1567)
at Scheduler._runNextTasks (util-2020.2.js:1571)
at Scheduler._runNextTasks (util-2020.2.js:1571)
at update (util-2020.2.js:1524)
window.onerror @ core-2020.2.js:1822
log4javascript.min.js:1 FATAL unknown | {}
BrowserConsoleAppender.append @ log4javascript.min.js:1

sorry to bother you again

I have rewritten the code
but still get an error

  • TypeError: Cannot read property ‘length’ of undefined
if ([0].includes(loop_pract.thisN)) {
jitters = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0];

}

function shuffle(a) { var j, x, i; for (i = a.length - 1; i > 0; i--) { j = Math.floor(Math.random() * (i + 1)); x = a[i]; a[i] = a[j]; a[j] = x; } return a;} 

shuffle(jitters);
current_jitter = jitters.pop();
thisExp.addData("jitter", current_jitter);

When I put the lines right within the loop

if ([0].includes(loop_pract.thisN)) {
jitters = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0];
shuffle(jitters);
current_jitter = jitters.pop();

}

thisExp.addData("jitter", current_jitter);

It does not show any error but only does the very first trial and then shows a black screen. I do not understand what the problem is…why I cant define the jitters…
Thank you!

If I write your code like this and run it in the browser console, it works fine. If you’re on Firefox or Chrome, you could try it out for yourself by pressing F12 and copy-pasting the code below. So, my guess is that jitters isn’t available when you need it. Why that is the case, I’m not sure. Printing debug output, as I shown in earlier comments, could give you some insight in what happens.

jitters = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0];
function shuffle(a) { var j, x, i; for (i = a.length - 1; i > 0; i--) { j = Math.floor(Math.random() * (i + 1)); x = a[i]; a[i] = a[j]; a[j] = x; } return a;} 
shuffle(jitters);
current_jitter = jitters.pop();