Javascript: Instead of automatically clicking next, how to code it to go to a certain question? | XM Community
Solved

Javascript: Instead of automatically clicking next, how to code it to go to a certain question?


Badge +1
Dear friends,

My friend wrote me some really nice code! I asked him to write some javascript code to create a timed test. They start a block of a 100 questions, but when it starts it starts a countdown. When the countdown reaches 0 the script automatically skips to the next question. This happens for all the questions that see that the timer stands at zero. However, with a 100 questions it goes through all the questions so quickly, it looks like it survey is crashing.

Is there a way to adjust this code to skip to a certain question instead if the timer reaches 0? Currently, it reads the script and goes to the next question in line, which then does the same. Is there a way to instead instruct Qualtrics to skip everything and go to my "Post-measure" question?

Qualtrics.SurveyEngine.addOnload(function()
{

function startTimer(duration, display) {

if (--timer < 0) {
clearInterval(myTimer);
timeOver();
}
}

var timerSeconds = parseInt("${e://Field/timeRemaining}");
display = document.querySelector('#time');

if (timerSeconds > 0) {
startTimer(timerSeconds, display);
}

var timeOver = function () {
document.getElementById("timer_1").innerHTML = "Time is up.";
$('NextButton').click();
x = 1;
var bgColor = setInterval(change, 1000);
}



});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Voer uw JavaScript hier uit wanneer de pagina volledig wordt weergegeven*/
var timerSeconds = parseInt("${e://Field/timeRemaining}");
if (timerSeconds === 0) {
$('NextButton').click();
}

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Voer uw JavaScript hier uit wanneer de pagina nog niet geladen is*/

});
icon

Best answer by TomG 2 May 2019, 18:23

View original

5 replies

Userlevel 7
Badge +27
Add display logic to all the questions to only display if timeRemaining is > 0 (BTW, I think there is a flaw in the JS because I don't see where timeRemaining is being updated).
Badge +1
Thanks a lot Tom!

When I test run it, it does seem to automatically initiate "click next button" command after time runs out. The time remaining is instead set in survey flow. I think that is how it works?

So you would change the last part like this? Sorry, I'm really new to coding! (this is something I found on the web)

var timerSeconds = parseInt("${e://Field/timeRemaining}");
Display Logic: Show only if timeRemaining > 0];
}
Hello @Yuri ,

Set an embedded data 'flag=1' from the JS once the timer reaches 0.
Using this embedded data add display logic on each question as display question if flag=0
Userlevel 7
Badge +27
> @Yuri said:
> Thanks a lot Tom!
>
> When I test run it, it does seem to automatically initiate "click next button" command after time runs out. The time remaining is instead set in survey flow. I think that is how it works?
>
> So you would change the last part like this? Sorry, I'm really new to coding! (this is something I found on the web)
>
> var timerSeconds = parseInt("${e://Field/timeRemaining}");
> Display Logic: Show only if timeRemaining > 0];
> }

You add display logic to the Qualtrics questions, not the JavaScript. Once that is done, you can remove the addOnReady function from the script.
Badge +1
It worked! Also without removing the addOnReady! Thank you sooooo much. I've been working on this for soo long. I can't believe it's finally working!

Leave a Reply