Integrating loop and merge and timing? | XM Community
Solved

Integrating loop and merge and timing?

  • 19 May 2020
  • 8 replies
  • 135 views

Hi there,
I'm looking for help on programming a study where I want to loop and merge a set of different fields within a 15 minute time frame. Ideally, I would want the loop to continue looping through the fields UNTIL it reaches 900 seconds (15 minutes).
I've tried to do this by adding a timing question with auto-advance after 900 seconds to the loop and merge block, but unfortunately this only sets an auto-advance time on one single loop. What I'm looking for is for a way to keep looping through the fields until the timer reaches 900.
Is there a way to do this? Thanks so much for your help!

icon

Best answer by TomG 20 May 2020, 04:21

View original

8 replies

Userlevel 7
Badge +27

Loop & merge loops a set number of times, and you can't set it to loop for a specific amount of time. So, to loop for 15 minutes you would have to set it to loop more times than could be reached in 15 minutes, then set an embedded variable within the loop using JavaScript. That embedded variable would be used in display logic on each question in the loop so that once 15 minutes was reached none of the questions in the remaining loops are displayed.

Thanks for the message, Tom! I'm quite new to JavaScript -- are there any resources for programming this into the loop and merge? Or do you have any suggestions about how to do this? Thanks!!

Userlevel 7
Badge +27

Qualtrics keeps a built-in survey timer in the embedded variable Q_TotalDuration. So you can set another variable (e.g., startLoop) to Q_TotalDuration just before the loop & merge block. Set another variable to flag timeouts to zero before the loop & merge.
In your loop you can pipe startLoop and Q_TotalDuration into your JavaScript to calculate the time elapsed and if it is more than 15 update timeout to 1. The JavaScript could be associated with one quesiton in the loop or one question per page depending on you needs.

Thanks TomG !
Here's the code I have (on the first question of the loop and merge page):
var start = parseInt('${e://Field/StartLoop}');
var end = parseInt('${e://Field/Q_TotalDuration}');
var elapsed = end - start;
Qualtrics.SurveyEngine.setEmbeddedData('elapsed', elapsed);
if (elapsed > 900) {
Qualtrics.SurveyEngine.setEmbeddedData('timeout', 1);
So the code is able to update the elapsed time and "timeout" variable for the initial loop, but it does not seem to update for each loop. I have this code under .addOnReady for the first question of the loop and merge.
Because it's not updating, I'm having some trouble then moving the survey forward and skipping the rest of the loop.
Your help is greatly appreciated!

Userlevel 7
Badge +27

Joyce,
I just did a quick test by piping Q_TotalDuration into a question text of a loop & merge question and the value changes as expected.
Have you checked the console for JS errors? Assuming StartLoop parses as a number, your code looks correct to me except for a missing } on the last if. Corrected (you don't need the {} if there is only one statement in the if):
var start = parseInt('${e://Field/StartLoop}');
var end = parseInt('${e://Field/Q_TotalDuration}');
var elapsed = end - start;
Qualtrics.SurveyEngine.setEmbeddedData('elapsed', elapsed);
if(elapsed > 900) Qualtrics.SurveyEngine.setEmbeddedData('timeout', 1);

Amazing, thanks so much @TomG !! It worked like a charm.
One last thing I'd like to add (ideally), is a timer that counts down across these different loop and merge pages, to give participants an idea of how much time has elapsed/remains.
I've adapted from a previous post with the following:
var start = parseInt('${e://Field/StartLoop}');
var end = parseInt('${e://Field/Q_TotalDuration}');
var elapsed = end - start;
Qualtrics.SurveyEngine.setEmbeddedData('elapsed', elapsed);
if(elapsed >30) Qualtrics.SurveyEngine.setEmbeddedData('timeout', 1);

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: 07:30"; 

header.appendChild(timer);
document.body.insertBefore(header, 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';
  display[text] = minutes + ":" + seconds;  
  if (--timer < 0) {  
  clearInterval(myTimer);  
  timeOver();  
  }  
 }, 1000);  
 }  
 var timerSeconds = 450-elapsed,  
 display = document.querySelector('#time');  
 startTimer(timerSeconds, display);  
 var timeOver = function() {  
 document.getElementById("timer_1").innerHTML = "Time is up.";  
 x = 1;  
 var bgColor = setInterval(change, 1000);  
 }  
The issue I'm having here is that a new timer appears under the old one for each loop and merge page such that by the 4th loop you see 4 timers with the same time remaining. Is there a way to just display one timer?
Thank you!

https://community.qualtrics.com/XMcommunity/discussion/comment/25573#Comment_25573Hi Tom,
What variable from the embedded data would I have to use for my display logic if I use this code in the first question of my loop?
var start = parseInt('${e://Field/StartLoop}');
var end = parseInt('${e://Field/Q_TotalDuration}');
var elapsed = end - start;
Qualtrics.SurveyEngine.setEmbeddedData('elapsed', elapsed);
if(elapsed > 900) Qualtrics.SurveyEngine.setEmbeddedData('timeout', 1);
Thank you so much.

Userlevel 7
Badge +27

https://community.qualtrics.com/XMcommunity/discussion/comment/42093#Comment_42093Use timeout

Leave a Reply