Custom code for reaction times not working | XM Community
Skip to main content

Hello everyone,
I commissioned a java developer to build the code below to record participants' reaction times, meaning how long it takes from the offset of a stimulus on screen to participant pressing a response key. Unfortunately the code does not work, as half of the times it exports the reaction times as 000, like the example below:
correct export: 1131,220,135,330,74,1697,1151,676,668,303,373
wrong export: 127,167,160,157,110,000,000,000,000,000,000
Below I share the full code. Any idea of why this might be the case will be really appreciated.

var t;
Qualtrics.SurveyEngine.addOnload(function() {
  /*Place your JavaScript here to run when the page loads*/
 jQuery("#Buttons").hide();
});
Qualtrics.SurveyEngine.addOnReady(function() {
  /*Place your JavaScript here to run when the page is fully displayed*/
 var qid = this.questionId;
jQuery("#"+qid+" .QuestionText").css("padding-bottom","0px");
jQuery("#"+qid+" .ChoiceStructure").css("border-spacing","3px 0px");
  var align = "${e://Field/alignment}";
  var time = parseInt("${e://Field/time}");
var curri = 0;
  console.log(time)
jQuery("#Buttons").hide();
  this.hideNextButton();
  this.hidePreviousButton();
  jQuery("#" + qid + " .QuestionBody").css("pointer-events", "none");
  var that = this;
  var c = 0;
var currchoice= "";
  var timer_is_on = 0;
  function timedCount() {
    c = c + 1;
    t = setTimeout(timedCount, 1);
  }
  function startCount() {
    c = 0;
    if (!timer_is_on) {
      timer_is_on = 1;
      timedCount();
    }
  }
  function stopCount() {
    clearTimeout(t);
    timer_is_on = 0;
  }
jQuery(document).on('keydown', function(event) {
    if (!timer_is_on) {
      return;
    }
    var key = event.key.toUpperCase();
    if (align == "left") {
      if (key == "A") {
        jQuery('inputUtype="checkbox"]:eq(1)').prop('checked', true); //No
currchoice = "0";
        jQuery("#NextButton").click();
} else if (key == "L") {
 jQuery('inputbtype="checkbox"]:eq(0)').prop('checked', true); //YES
currchoice = "1";
        jQuery("#NextButton").click();
      }
    } else {
      if (key == "A") {
        jQuery('input currchoice = "1";
        jQuery("#NextButton").click();
      } else if (key == "L") {
        jQuery('inputrtype="checkbox"]:eq(1)').prop('checked', true); //NO
currchoice = "0";
        jQuery("#NextButton").click();
      }
    }
console.log(currchoice);
  });
  var targetid = "imgr";
  if (align == "left") {
    var el = jQuery('#' + qid + ' .LabelContainer:eq(0)').detach();
    jQuery("#" + qid + " .ChoiceStructure tbody tr").append(el);
    jQuery(".no").text('A');
    jQuery(".yes").text('L');
    targetid = "imgl";
    jQuery("#imgr").attr('src', "${lm://Field/1}");
   jQuery("#"+targetid).on('load', function() {
if(curri==0)return;
       setTimeout(function() {
curri=0;
                jQuery("#"+targetid).show().attr("src", "https://cardiffmet.eu.qualtrics.com/ControlPanel/Graphic.php?IM=IM_0iyWXtOyRB7QZqR");
        jQuery("#" + qid + " .QuestionBody").show();
        startCount();
      }, time);
    });
curri = 0;
        jQuery("#" + targetid).attr("src", "https://cardiffmet.eu.qualtrics.com/ControlPanel/Graphic.php?IM=IM_87CytKMbs8ODCbX");
    setTimeout(function() {
curri = 1;
      jQuery("#" + targetid).attr('src', "${lm://Field/2}");
    }, 1000);
  } else {
jQuery("#imgl").attr('src', "${lm://Field/1}");
    jQuery("#"+targetid).on('load', function() {
//herererere
if(curri==0)return;
       setTimeout(function() {
curri=0;
                jQuery("#"+targetid).show().attr("src", "https://cardiffmet.eu.qualtrics.com/ControlPanel/Graphic.php?IM=IM_0iyWXtOyRB7QZqR");
        jQuery("#" + qid + " .QuestionBody").show();
        startCount();
      }, time);
    });
curri = 0;
        jQuery("#" + targetid).attr("src", "https://cardiffmet.eu.qualtrics.com/ControlPanel/Graphic.php?IM=IM_87CytKMbs8ODCbX");
    setTimeout(function() {
curri = 1;
      jQuery("#" + targetid).attr('src', "${lm://Field/2}");
    }, 1000);
    //jQuery("#imgr").attr('src',"${lm://Field/2}");
      }
  jQuery("#NextButton").on('click', function() {
    var s = "${e://Field/T3BL1_Sequence}";
    var arr;
    if (s == "") {
      arr = e];
    } else {
      arr = s.split(",");
    }
    arr.push("${lm://Field/3}");
    Qualtrics.SurveyEngine.setEmbeddedData("T3BL1_Sequence", arr.toString());
s = "${e://Field/T3BL1_ReactionTime}";
    arr=u];
    if (s == "") {
      arr = ];
    } else {
      arr = s.split(",");
    }
    arr.push(c);
    Qualtrics.SurveyEngine.setEmbeddedData("T3BL1_ReactionTime", arr.toString());
s = "${e://Field/T3BL1_Choices}";
    arr=d];
    if (s == "") {
      arr = .];
    } else {
      arr = s.split(",");
    }
    arr.push(currchoice);
    Qualtrics.SurveyEngine.setEmbeddedData("T3BL1_Choices", arr.toString());
  });
});
Qualtrics.SurveyEngine.addOnUnload(function() {
  /*Place your JavaScript here to run when the page is unloaded*/
  clearTimeout(t);
  jQuery(document).off("keydown");
});

Any reason you can't use the built in timing question to get what you want? I'm not a coder so won't be able to find the mistake for you. Just a fan of using built-in features over custom code when you can.

https://www.qualtrics.com/support/survey-platform/survey-module/editing-questions/question-types-guide/advanced/timing/


bstrahin thanks for your answer. We could not use it due to the fact that we are interested in the laps of time that goes from the stimulus disappearing from the screen till the participant presses the response


Do you have a variable that can track when the stimulus appears? Could you use math between those columns to track time of click - delay in appearance?


Leave a Reply