Survey/quiz time limit | XM Community
Skip to main content

Good morning,
I am trying to set a 45-minute time limit for a survey/quiz, after which a user is automatically taken to the end of the survey. I came across the code below in another discussion, but the timer stops counting down when I move away from the page, and so, it does not count time spent on the survey accurately. Is there a way to set the time limit and keep track of time, even if the user is away from the survey page? Perhaps, there is a simpler way to accomplish this that I am not seeing.
Qualtrics.SurveyEngine.addOnload(function()
{
   var headerCont = document.createElement("div");  
 headerCont.className = "header-cont";  
 headerCont.id = "header_container";  
 var header = document.createElement("div");  
 header.className = "header"  
 header.id = "header_1";  
 var timer = document.createElement("div");  
 timer.className = "timer";  
 timer.id = "timer_1";  
 timer.innerHTML = "Time Remaining: 45:00"; 
 headerCont.appendChild(header);  
 header.appendChild(timer);  
 document.body.insertBefore(headerCont, document.body.firstChild);
function startTimer(duration, display) {  
  var timer = duration, minutes, seconds;  
  var myTimer = setInterval(function() {  
   minutes = parseInt(timer / 60, 10)  
   seconds = parseInt(timer % 60, 10);  
   minutes = minutes < 10 ? "0" + minutes : minutes;  
   seconds = seconds < 10 ? "0" + seconds : seconds;  
   var text = ('innerText' in display)? 'innerText' : 'textContent';
  displayttext] = minutes + ":" + seconds;  
   if (--timer < 0) {  
    clearInterval(myTimer);  
    timeOver();   
   }  
  }, 1000);  
 }  
 var timerSeconds = 2700,  
 display = document.querySelector('#time');  
 startTimer(timerSeconds, display);  
 var timeOver = function() {  
     document.getElementById("timer_1").innerHTML = "Time is up.";

Qualtrics.SurveyEngine.setEmbeddedData("blockTimeFlag", "1");   
$('NextButton').click();
}

Hi rober, the first QTip here might be helpful, maybe you could use the mentioned Data element to end the survey.
Lucas


Does anyone have any leads to how one might deal with this type of issue?
I have been having similar problems. If I add event listeners to detect if the window is out of focus, my javascript timer does not work properly. Even if I get it to count down appropriately, it does not autoforward the respondent to "end survey" once I return to the survey and time is up.
Best
Ing


https://community.qualtrics.com/XMcommunity/discussion/comment/48722#Comment_48722If you are interested, I have a more robust countdown timer function that works if the respondent moves to a different tab or app. It even works if the respondent closes the browser tab/window.
See the link provided above for more info.


I've updated the countdown timer function so that it always displays the accurate time remaining even if the respondent closes the browser window then later comes back to the survey.


Thank you, Tom. I haven't had a chance to try out the function yet, but it looks like a solution to the issue I encountered,


Leave a Reply