Executing experimental items using Javascript?
Hi all,
I'm designing a color-shape switching task in OSWeb, but it turns out I need some help with Javascript.
In my experiment, I want the practice loop to repeat when participants score under a certain percentage. In a previous version of my experiment, I successfully programmed this using Python. To repeat the practice loop I inserted an inline_script after the practice loop and told the script to execute the practice_loop again using the items.execute() function when var.repeat is set to 1. I tried programming this using Javascript (see below), but when running this code, the debugging console tells me that the 'items' function is undefined. Am I correct in assuming that I can't call the 'items' function with Javascript? If yes, is there another way to program this using Javascript? Or is there a (probably obvious) error in my code that prevents the script from running correctly?
inline_javascript after the 'keyboard_response' item to track accuracy in the trial sequence
// TRACK ACCURACY: Repeat practice? if (vars.acc >= 75){ vars.repeat = 0 } else if (vars.acc < 75){ vars.repeat = 1 }
inline_javascript after the practice_loop to repeat the practice block
if (vars.repeat == 1){ // if the practice needs to be repeated // show instruction screen again if (vars.task === "shape"){ // when current task is shape items.execute("shape_intro"); // run shape instructions } else if (vars.task === "color"){ // when current task is color items.execute("color_intro"); // run color instructions } else if (vars.task === "mixed"){ // when current task is mixed items.execute("mixed_intro_2"); // run mixed instructions } items.execute("reset_feedback"); // always run reset_feedback item if (vars.task === "shape" || "color"){ // if the task is shape or color items.execute("single_prac_loop"); // run single_prac_loop } else if (vars.task === "mixed"){ // if the task is mixed items.execute("mixed_prac_loop"); // run mixed_prac_loop } } else if (vars.repeat == 0){ // continue as normal // pass }
I attached the .osexp so you can hopefully get an idea of what I'm referring to (somehow I'm not able to indent the code in this post). Let me know if you need any further information!
Comments
I think you could use a 'repeat_cycle' item in the experiment structure and add a condition to its run-if statement in the sequence item. E.g. run if: [repeat] == 1. Good luck!
Hi @favdberg ,
Am I correct in assuming that I can't call the 'items' function with Javascript?
Yes, this is correct. See also here:
If yes, is there another way to program this using Javascript?
Yes, as @JasperdeWaard suggested, you could use the repeat variable in a
repeat_cycle
item. However, you will probably have to redesign the hierarchy of your experiment a bit, such that the practice phases have an extraloop
-sequence
wrapped around them. Like so:Next, as Jasper suggested, you can set "Repeat if" to
When looking at your script, I also noticed a few other things:
Prepare tab
of yourinline_javascript
itemvars.correct.response
should bevars.correct_response
)feedback
item rather than asketchpad
item. See also here: https://osdoc.cogsci.nl/3.3/manual/stimuli/visual/#the-difference-between-sketchpad-and-feedback-itemsI attached an updated script. I only looked at the first practice round.
Hope this helps!
Cheers,
Lotje
Did you like my answer? Feel free to
Awesome! Thanks so much @JasperdeWaard and @lvanderlinden for responding so quickly and for taking the time to adjust my script. Everything seems to be running as it should now :)
Great to hear! :)
Did you like my answer? Feel free to