Execution queue of setEmbeddedData() in addOnload within a loop? | XM Community
Skip to main content
Hi everyone,

I have a loop repeating n questions in random order for t times. The questions contain references to embedded fields. The embedded fields need to be changed in each iteration of the loop. The fields are not pre-populated.



I use javascript in addOnload() to change the embedded field data. What happens is that in the first iteration of the loop the embedded fields are not set. In the second iteration the fields are set.



It seems that addOnload() executes the setting of the embedded data only after the page loads (?).



Pseudocode to illustrate



For (t in times) {

set embedded data field of question n_i

display question n_i

set embedded data field of question n_i + 1

display question n_1 + 1

...

}



I cannot pre-populate the fields. I tried hiding the first loop iteration and using t+1 loop iterations, but this does not execude the code in iteratio no 1.



Any recommendations?



I am happy to use javascript directly without the embedded fields, i don't mind. 😃
Embedded data fields don't get set until they get sent to the server (i.e., you go to the next page). I'm not clear on what your embedded fields actually are or what they are being set to. One solution would be add a text display question to the beginning of the block followed by a page break, then add the JS that sets the embedded data field to that script and then have it click the next button.
Hi, I will try a text display question, that sounds fair.



My workaround for now is to hide the first iteration of the loop by javascript with the usual code



var j = parseInt( "${lm://CurrentLoopNumber} ") - 1; // current loop iteration

if ( j == 0 ) {

jQuery("#"+this.questionId).hide();

this.clickNextButton();

}



FYI: Background is a randomized multi-value multi-domain task (no need to answer, just out of completeness): I am showing multi-valued stimuli (e.g. 0,0,0, then 0,0,1; then 0,1,0; etc.) where the numerical features correspond to some actual representation from a domain (e.g. 0,0,0 may be "small, red, triangle", and 0,0,1 may be "small, red, square", for the domain "shapes"). There are multiple domains ("shapes", "animals", etc.). with three features each. I randomize the order of the stimuli shown for each domain. The participant views all domains, but different stimuli per domain; then again all domains but different stimuli per domain; etc. until she has seen all stimuli from each domain. The tricky bit is that the order of stimuli per domain is random and different across domains.

I now implement this by creating a JS array holding the numerical stimuli in one fixed order [000, 001, 010, ...]. Then creading an order index per domain, and shuffling a copy of this index one order for each domain. Moreover I have set up different feature arrays per domain that hold the words that correspond to the feature values 0 and 1 in each domain and for each feature [["small", "large"], ["red", "green"], ["square", "triangle"]], etc. These constants are set up before the experiment begins. I create a loop that loops repeatedly through all domains. Then in each trial of the loop for each domain I just get the current trial's representation for the stimulus that is to be shown according to the random order with something like this ` features_of_domain[numerical_stimuli_in_fixed_order[shuffled_order_of_domain[t]]`, where t is the loop iterator. All arrays are set up as embedded texts in delimiter-separated string format at the beginning. This works like a charm.



It's a bit cumbersome, but I don't think qualtrics can store arrays in the background, can it?

Leave a Reply