updating trial variable

639 views
Skip to first unread message

Gar O'Connell

unread,
Sep 3, 2015, 11:16:43 AM9/3/15
to jsPsych
Hi Josh,

Thanks for very useful tool. I am stuck on a basic issue - updating the value of a variable ("k" which indexes elements of stimulus array) between trials. Right now I just want to do something basic like switch it from 0 to 1 from the first to second trial, and I will add more complexity once I can do that. I thought the on_finish function would help, but it does not seem to pass back into the stimuli parameter. Below is my code:

Thanks for help,

Garret

var k = 1;
var optdelay = ["NOW", "1 MONTH"];
var optamount = ["50", "100"];
   /* define test block */

var block = {
      type: "single-stim",
      stimuli: ["<div class='left center-content'><p style=color:white>" + optdelay[k] + optamount[k] + "</p>"],
choices: [37,39],
on_finish: function(){
var k = 0;
},
is_html: true
}

var timeline = [];
var while_loop_chunk = {
chunk_type: 'while',
    timeline: [block],
      continue_function: function(data) {
var response = JSON.parse(data[0].key_press); // convert JSON to array 
if (response[0] == 37) { 
 return false; // continue the loop
       } else { 
 return true;
       }
}

Josh de Leeuw

unread,
Sep 3, 2015, 11:22:10 AM9/3/15
to Gar O'Connell, jsPsych
Hi Garret.

The issue is that the block variable is evaluated before the experiment runs. So you are updating k correcty in on_finish(), but optdelay[k] is selected before the experiment runs.

You can get around this by using a function as the parameter value (http://docs.jspsych.org/features/functions-as-parameters/).

It would look like this:

var block = {
  type: "single-stim",
  stimuli: [function(){
    return "<div class='left center-content'><p style=color:white>" + optdelay[k] + optamount[k] + "</p>";
  }],
choices: [37,39],
on_finish: function(){
var k = 0;
},
is_html: true
}

By wrapping the parameter value in a function, jsPsych will wait until the trial begins to evaluate the function, and use whatever value the function returns as the parameter.

--Josh

--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+u...@googlegroups.com.
To post to this group, send email to jsp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jspsych/2bc4555c-ea85-4b42-8c36-6e7a21a068a8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gar O'Connell

unread,
Sep 3, 2015, 12:50:23 PM9/3/15
to jsPsych
Hi Josh,

thanks for the advice. The code you edited runs fine, but again the variable does not update (i.e. the k=0 values of optdelay and optamount are not being displayed in trials). Also, the exit response (anything but arrow key 37) is not stopping the while chunk. Sorry for badgering. I just threw this code together from tutorials/forum so I am sure I have made a very basic error. Here is full code:

Thanks again.

<!doctype html>
<html>
  <head>
    <title>test</title>
    <script src="jspsych.js"></script>
    <script src="plugins/jspsych-text.js"></script>
<script src="plugins/jspsych-single-stim.js"></script>
    <link href="css/jspsych.css" rel="stylesheet" type="text/css"></link>
  </head>
  <body bgcolor=black>
  </body>
  <script>

    /* define welcome message block */
    var welcome_block = {
      type: "text",
      text: "<p style=color:white>Welcome to the experiment. Press any key to begin.</p>"
    };

var k = 0;
var optdelay = ["NOW", "1 MONTH"];
var optamount = ["50", "100"];
   /* define test block */

var block = {
 type: "single-stim",
 stimuli: [function(){
   return "<div class='left center-content'><p style=color:white>" + optdelay[k] + optamount[k] + "</p>";
 }],
 choices: [37,39],
 on_finish: function(){
var k = 1;
},
 is_html: true
}

var timeline = [];
var thispress = 0;
var while_loop_chunk = {
chunk_type: 'while',
    timeline: [block],
      continue_function: function(data) {
var response = JSON.parse(data[0].key_press); // convert JSON to array 
if (response[0] == 39) { // if the response is R
 return false; // continue the loop
       } else { 
 return true;
       }
}
}
timeline.push(while_loop_chunk);
    
/* create experiment definition array */
//var experiment = [while_loop_chunk]; 
    //experiment.push(welcome_block);
    //experiment.push(instructions_block);
    //experiment.push(trial);

    /* start the experiment */
    
jsPsych.init({
experiment_structure: [welcome_block,while_loop_chunk],
      on_finish: function() {
        jsPsych.data.displayData();
      }
    });
  </script>
</html>

Gar O'Connell

unread,
Sep 4, 2015, 6:18:44 AM9/4/15
to jsPsych, gar...@hotmail.com
Hi Josh,

I figured out why it wasn't updating. Apparently already declared variables cannot be overwriting, so the individual elements need replacing e.g. instead of "var k = 1", "k[0] = 1". Thanks for help.

Best,

Garret
Reply all
Reply to author
Forward
0 new messages