Adding timer to a block+Display countdown+autoadvance+timesupmessage (URGENT!) | XM Community
Question

Adding timer to a block+Display countdown+autoadvance+timesupmessage (URGENT!)

  • 1 August 2020
  • 0 replies
  • 93 views

Hi,
I need some urgent help with this to run my survey by a deadline for my PhD. I have looked up many similar questions on this community but am facing some roadblocks:
I have a block of 10 questions (separated into 10 pages) which I want my respondents to answer in 4 minutes. I want to show them the countdown timer and I want them to autoadvance to the next block if time is up, even if they haven't finished answering. I also want to show them a "Time is Up!" message. I am new to JS, so help me out!!
I have done the following:
1) I added the variable "timeRemaining" = 240 as embedded data in the survey flow in the block preceding the block of questions I want to time
2) I added the below JavaScript to the first question of the block. This works great in kicking people out of this block in 240 seconds but the timer is not displayed on each page in the block. How do I display the timer? Also, how can I add a "Tme's up" message to this?
*********************
Qualtrics.SurveyEngine.addOnload(function()
{
Qualtrics.SurveyEngine.addOnload(function()
{
 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: 00:10"; 

header.appendChild(timer);
document.body.insertBefore(header, document.body.firstChild);

function startTimer(duration, display) {  
 var timer = duration, minutes, seconds;  
 var myTimer = setInterval(function() {  
    Qualtrics.SurveyEngine.setEmbeddedData('timeRemaining', timer);
  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';
  display[text] = minutes + ":" + seconds;  

  if (--timer < 0) {  
  clearInterval(myTimer);  
  timeOver();  
  }  
 }, 1000);  
 }  
var timerSeconds = parseInt("${e://Field/timeRemaining}");  
 display = document.querySelector('#time');  
 startTimer(timerSeconds, display);  
 var timeOver = function() {  
 document.getElementById("timer_1").innerHTML = "Time is up.";  
   $('NextButton').click();
 }  
});

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/

});

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

});
************************************************************************


0 replies

Be the first to reply!

Leave a Reply