adding javascript | XM Community
Skip to main content

I want to have a counter display that counts up by 27 seconds each second. I have this JavaScript code...but I can't get it to work in Qualtrics. I am guessing I am missing something that Qualtrics requires? Any advice?
Thanks so much!

Qualtrics.SurveyEngine.addOnload(function()
{
var seconds = 0;
   var el = document.getElementById('seconds-counter')
   function incrementSeconds() {
   seconds += 27;
   el.innerText = "You have been here for " + seconds + " seconds.";
var cancel = setInterval(incrementSeconds, 1000);
   });
});

Hi there, if you still need, I think I found the codepen you used to get this started and the below is working for me.
First, add the below to the QuestionText using the Rich Content Editor's HJTML/Source view "<>":

 

Then, add the following to the question's JavaScript:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var seconds = 0;
var el = document.getElementById('seconds-counter');

function incrementSeconds() {
  seconds += 27;
  el.innerText = "You have been here for " + seconds + " seconds.";
}

var cancel = setInterval(incrementSeconds, 1000);
});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
   clearInterval(incrementSeconds);
});


Leave a Reply