Java timer data not correctly recorded in embedded data - it cumulates time from future questions | XM Community
Skip to main content

I programmed a timer for each question which captures the time people spend on the question. I could not use Qualtrics in built timer because I want people to be able to pause the timer. The data in seconds for each question should be added to an embedded variable called 'response_time'. I have created new embedded variable to record timer data for each question.
Q embedded var
Q1 response_time
Q2 response_time2
Q3 response_time3
The results are forward cumulative, and only the last question has its time data correctly recorded in embeded variable. The result is the following -
response_time = timer value from Q1 + timer value from Q2 + timer value from Q3
response_time2 =timer value from Q2 + timer value from Q3
response_time3 = timer value from Q3
I do not want other response times to be added. I even renamed all common variables to ensure there is no cross referencing. How can I get around it?
I am attaching the code here -
JAVA
Qualtrics.SurveyEngine.addOnload(function()
{
var h2 = document.getElementsByTagName('h2')[0],
  start = document.getElementById('start2'),
  stop = document.getElementById('stop2'),
  clear = document.getElementById('clear'),
timerecord = document.getElementById('timerecord2'),
  seconds_2 = 0, minutes_2 = 0, hours_2 = 0,
  t;

function add2() {
  seconds_2++;
  if (seconds_2 >= 60) {
    seconds_2 = 0;
    minutes_2++;
    if (minutes_2 >= 60) {
      minutes_2 = 0;
      hours_2++;
    }
  }
   
  h2.textContent = (hours_2 ? (hours_2 > 9 ? hours_2 : "0" + hours_2) : "00") + ":" + (minutes_2 ? (minutes_2 > 9 ? minutes_2 : "0" + minutes_2) : "00") + ":" + (seconds_2 > 9 ? seconds_2 : "0" + seconds_2);

  timer2();
}

function timer2() {
  t = setTimeout(add2, 1000);
var time2 = seconds_2 + minutes_2*60 + hours_2*3600;
Qualtrics.SurveyEngine.setEmbeddedData( 'response_time2', time2);
jQuery("#infodiv2").hide();
jQuery("#start2").hide();
jQuery("#stop2").show();
//jQuery(timerecord).hide();
}

timer2();
/* Start button */
start.onclick = timer2;

/* Stop button */
stop.onclick = function() {
  clearTimeout(t);
jQuery("#infodiv2").show();
   jQuery("#stop2").hide();
jQuery("#start2").show();
//jQuery(timerecord).hide();
}

/* Clear button */
clear.onclick = function() {
  h2.textContent = "00:00:00";
  seconds_2 = 0; minutes_2 = 0; hours_2 = 0;
}

});
HTML


  



Any help is appreciated!! Thanks

Not exactly sure what you mean by forward cumulative. Does this mean someone would start question 1 at 00:00:00, spend 15 seconds on the question, then move to question 2, and the timer would start at 00:00:15?
If they all are meant to start at 0, your timer works perfectly fine apart from the clear button not being present in the HTML; I would simply add some JS to calculate the cumulative time after the fact, for example:
Qualtrics.SurveyEngine.addOnload(function()
{
const time1 = parseInt('${e://Field/response_time1}')
const time2 = parseInt('${e://Field/response_time2}')
const time3 = parseInt('${e://Field/response_time3}')

const cumulative_seconds = time1 + time2 + time3
Qualtrics.SurveyEngine.setEmbeddedData('total_time',cumulative_seconds)


});
I also don't think you need to worry about cross referencing UNLESS all questions are meant to be on the same page, in which case I would restructure the code to be in one question instead of in each question. If you mean to have two timers going, one cumulative, and one starting at zero, then I would initialize two setInterval functions with one timer initialized at 0 seconds and one timer initialized at the cumulative time variable, where the first timer would update response_timeX and the other would update response_time_total for example. Attached is a QSF.
AnsweringForumQuestions (1).qsfLet me know if you can clarify the functionality you are looking for.


Leave a Reply