Creating a dynamic slider using setInterval | XM Community
Skip to main content

Hello,

 

I've been trying to create JavaScript that records the value from a slider question every 2 seconds using the setInterval() function. However, Qualtrics is currently not recording the data, and seems to be having difficulties accessing the slider values. I've checked numerous times, and tried with edited question names, with graphic and normal sliders, all with no success. I've attached a copy of the code below, along with the errors from the web developer at both the user end and builder end. Any guidance would be hugely appreciated; thank you!

 

 

@Tarsha Let me guess, you get the code from chatGPT? It’s not work, the variable define should be inside a function. That’s the first thing i notice


Thanks Nam, I didn’t originally but I was resorting to trying to troubleshoot with ChatGPT, which I since realised messed up the code more than fixing it. My current layout is:

var timer;
var tickCount;

var SliderValues = =];

Qualtrics.SurveyEngine.addOnload(function() {
 // the function called by the timer on every tick. Used to record the slider value at that time.
function updateTime() {
// increase the tick count by one - used to store each slider value uniquely
tickCount++;

// get the current slider value (for the slider - currently known as 1)
// var sliderValue = this.getChoiceValue("QID1_1");
     

// alternatively this may be how you retrieve the slider value? (as a number in this case)
var sliderValue = parseInt(jQuery("#"+this.questionId+" .ResultsInput:eq(0)").val())
     if (sliderValue !== null && sliderValue !== undefined) {
      // Add the current slider value to the array
      sliderValues.push(sliderValue);
      console.log("Slider Value:", sliderValue);
      console.log("Tick Count:", tickCount);
    } else {
      console.warn("Slider value is null or undefined.");
    }

// Every tick of the timer this should record the slider data in qualtrics as:
//     SliderValue_1 = "answer1"
//     SliderValue_2 = "answer2"
//     SliderValue_3 = "answer3"
//     SliderValue_4 = "answer4"
            Qualtrics.SurveyEngine.setEmbeddedData('SliderValue_' + tickCount, sliderValue);
}
 
 //set the id of the slider so we can refer to it in the timer
 var sliderId = "1";
 
 // the first call to updateTime() here is used to set the startTime.
 tickCount = 0;

      timer = window.setInterval(function() {
            updateTime();
      }, 1000); // update every second (These are milliseconds)
});

Qualtrics.SurveyEngine.addOnUnload(function() {
  // Stop the timer
  clearInterval(timer);

  // Store all slider values as a JSON string in a single embedded data field
  Qualtrics.SurveyEngine.setEmbeddedData('AllSliderValues', JSON.stringify(sliderValues));
});

 

When you’re referring to the variable needing to be defined in the function, is that the variables currently defined globally? (i.e., timer, tickcount, sliderValues)?

 

Thanks so much! I’m very new to JavaScript so apologies if the questions are a bit obtuse.


Leave a Reply