Breaks after 10 loops in block with loop & merge function on | XM Community
Skip to main content

Hi all,
I have created a block consisting of 1 random selected video and 12 questions. I inserted the loop and merge function to random select 1 video of the 1045 total videos. In total there are 105 loops for every participant, so every participant watches 105 random selected videos and answers the corresponding questions. I want to insert breaks so that after 10 loops, participants can have a break and then continue with the next 10 loops until the full 105 loops are performed. Any ideas on how to this?

In the loop and merge block, on page 1, create a "break question". Then use the following JS to only show it when the loop and merge count is divisible by 10:
Qualtrics.SurveyEngine.addOnload(function () {
    let ln = "${lm://CurrentLoopNumber}";
    let quest = this;
    if (!(ln % 10)) {
        quest.disableNextButton();
        setTimeout(() => {
            quest.enableNextButton();
        }, 5000); //This controls how long the next button is disabled for (in ms)
    } else {
        quest.questionContainer.hide();
        quest.clickNextButton();
    }
});


He. In the block, I inserted a break question and inserted the JS. However, when I examine the preview, the block is constantly tripping.
Also, the break question is constantly visible and not only in every 10th loop. Any thoughts about this?
The video and 12 questions are on 1 page and I also have randomization on for the 12 questions.


Your block structure should be like this:
-------
Break Questions
Page Break
Video
Video Questions
-------
And the JS will go ONLY into the break question.
See if it works with this.


Yes, I already have that structure and the JS only in the break question.


You must be doing something different.
See this demo.


He AhmedA,

The code is indeed working when I don't randomize my questions.
I did randomization as follow using advanced randomization: The break question and the video are fixed as the first elements. The 12 questions are randomized. So the problem is that the randomizer ignores my page break and therefore the code is not working.
How did you solve this in the demo?


Okay. I hadn't taken the randomization into account. It basically breaks the code.
Remove the randomization and add this JS to any one question on page 2. It should do the job for you:
Qualtrics.SurveyEngine.addOnReady(function () {
    let first_quest = document.getElementById(Object.keys(Qualtrics.SurveyEngine.QuestionInfo)t0]);
    Object.keys(Qualtrics.SurveyEngine.QuestionInfo)
        .map((a) => document.getElementById(a))
        .slice(1)
        .map((a) => ({ sort: Math.random(), value: a }))
        .sort((a, b) => a.sort - b.sort)
        .map((a) => a.value)
        .forEach((a) => first_quest.insertAdjacentElement("afterend", a));
});


Thank you! It works perfectly!


Leave a Reply