Another auto-advance question | XM Community
Skip to main content
Hello guys,



So, I have a set of 20 questions (each on a separate site), where participants have 5 minutes to work on it. We already implemented this by declaring the timer variable as embedded data and substracting the time from this timer with the following code:



Qualtrics.SurveyEngine.addOnload(function()

{

var timeRemaining = parseInt("${e://Field/timeRemaining}");

var timer = setInterval(function() {

if(timeRemaining <= 0) {

clearInterval(timer);

$('NextButton').click()

}

timeRemaining--;

Qualtrics.SurveyEngine.setEmbeddedData('timeRemaining', timeRemaining);

}, 1000);

});



By then adding a DisplayCondition to all questions which require the embedded counter to be larger than 0, we can skip all following questions and proceed to the point where we want. This works pretty good without force response.



There is, however, still one big problem. This solution does not work with force response. Once the above code executes NextButton.click, qualtrics gives an error that there should be an answer in that box. Moreover, even after giving an answer (after this error occurred), qualtrics will skip to the end of the survey instead of to the next block without a displayCondition (which is weird?).



Since we need force response to the questions, it would be nice to find a workaround.



I was thinking about including a piece of code that sets an answer for the question (like -999) and then the force response mechanism will accept this as a response? I am not sure how to implement this but the API provides some commands:



setChoiceAnswerValue

setChoiceValue

setChoiceValueByRecodeValue

setChoiceValueByVariableName



So my idea is to include one of those commands to the code of each question, thereby set response to the question before executing the button.



Is this possible? How would the code look like (I tried but not manage). Or is there another solution?



Thanks!

Jonas
To set an answer to a question, the answer has to be a choice. You would have to include your "No answer" choice in each question and hide it.



You should move clearInterval to the unload function in case the person answers before the time expires.
> @TomG said:

> To set an answer to a question, the answer has to be a choice. You would have to include your "No answer" choice in each question and hide it.

>

> You should move clearInterval to the unload function in case the person answers before the time expires.



Hi TomG



Thank you very much for your answer. This might work. Can you share more details on how to implement it? As far as I understand it, we would have a text-box question (as it is now) and adding to the current code a hidden choice answer inside it's JavaScript menu.



The force response mechanism would then accept either a text-box answer or a choice answer, right?



Can you share the code?



Thank you so much!

Jonas
> @Jonas said:

> Thank you very much for your answer. This might work. Can you share more details on how to implement it? As far as I understand it, we would have a text-box question (as it is now) and adding to the current code a hidden choice answer inside it's JavaScript menu.

If all you have is text entry questions, you can just update the value of the text input with -999 then click the next button. I was assuming you would have some MC questions.

```

jQuery("#"+this.questionId+" .InputText").val("-999");

```
> @TomG said:

> > @Jonas said:

> > Thank you very much for your answer. This might work. Can you share more details on how to implement it? As far as I understand it, we would have a text-box question (as it is now) and adding to the current code a hidden choice answer inside it's JavaScript menu.

> If all you have is text entry questions, you can just update the value of the text input with -999 then click the next button. I was assuming you would have some MC questions.

> ```

> jQuery("#"+this.questionId+" .InputText").val("-999");

> ```

>

>





Hi TomG



Thank you, again for your answer. I tried to incorporate your feedback and ended up with this code:



Qualtrics.SurveyEngine.addOnload(function()

{

var timeRemaining = parseInt("${e://Field/timeRemaining}");



var timer = setInterval(function() {

if(timeRemaining <= 0) {

jQuery("#"+this.questionId+".InputText").val("-999");

$('NextButton').click()

}

timeRemaining--;

Qualtrics.SurveyEngine.setEmbeddedData('timeRemaining', timeRemaining);

}, 1000);

});



Qualtrics.SurveyEngine.addOnUnload(function()

{

var timer = setInterval(function() {

if(timeRemaining <= 0) {

clearInterval(timer);

}

}, 1000);

});



However, the script keeps pushing the next button and Qualtrics asks for a input to the question. I think there is still some bug in the code.



I feel that there is not much left! Thank you in advance for your help.

Jonas
'this' inside the setInterval function doesn't refer to the question object. Create an input variable before the function, then use the input variable inside the function to set the value.



Don't create a duplicate setInterval function in unload. Move unload inside onload so you can refer to the timer variable.
> @TomG said:

> 'this' inside the setInterval function doesn't refer to the question object. Create an input variable before the function, then use the input variable inside the function to set the value.

>

> Don't create a duplicate setInterval function in unload. Move unload inside onload so you can refer to the timer variable.



Hi Tom



Thanks you for your answer. I tried around for quite some time but I am not used to JavaScript and can't get it to work. The best I came up with is this:



Qualtrics.SurveyEngine.addOnload(function()

{

var timeRemaining = parseInt("${e://Field/timeRemaining}");

var ID = this;



var timer = setInterval(function() {

if(timeRemaining <= 0) {

jQuery('#'+ID.questionId+'.InputText').val('-999');

$('NextButton').click()

}

timeRemaining--;

Qualtrics.SurveyEngine.setEmbeddedData('timeRemaining', timeRemaining);

}, 1000);

});



It seems that the question object is not passed into the ID variable. I think that I am quite off here right now 😞



Can you give me additional hints?



Thanks

Jonas
> @Jonas said:

> Can you give me additional hints?

```

Qualtrics.SurveyEngine.addOnReady(function()

{

var timeRemaining = parseInt("${e://Field/timeRemaining}");

var input = jQuery("#"+this.questionId+" .InputText");



var timer = setInterval(function() {

if(timeRemaining <= 0) {

input.val('-999');

jQuery('#NextButton').click()

}

timeRemaining--;

Qualtrics.SurveyEngine.setEmbeddedData('timeRemaining', timeRemaining);

}, 1000);



Qualtrics.SurveyEngine.addOnUnload(function() {

clearInterval(timer);

});

});

```
> @TomG said:

> > @Jonas said:

> > Can you give me additional hints?

> ```

> Qualtrics.SurveyEngine.addOnReady(function()

> {

> var timeRemaining = parseInt("${e://Field/timeRemaining}");

> var input = jQuery("#"+this.questionId+" .InputText");

>

> var timer = setInterval(function() {

> if(timeRemaining <= 0) {

> input.val('-999');

> jQuery('#NextButton').click()

> }

> timeRemaining--;

> Qualtrics.SurveyEngine.setEmbeddedData('timeRemaining', timeRemaining);

> }, 1000);

>

> Qualtrics.SurveyEngine.addOnUnload(function() {

> clearInterval(timer);

> });

> });

> ```

>

>



Works like a charm. Thank you so much!!

Leave a Reply