How can I add a timeout message for individual questions? | XM Community
Skip to main content
I have a serious of questions that are all presented on different pages. On each page, I have also added a "Timing" item to display a countdown clock. If the countdown clock reaches zero, I want an error message to pop up. Currently I am using the following code for each Question Javascript to show an error message after 4 seconds:



Qualtrics.SurveyEngine.addOnload(function()

{

/*Place your JavaScript here to run when the page loads*/

});



Qualtrics.SurveyEngine.addOnReady(function()

{

setTimeout(function(){

alert("Time is up. Please choose an answer and move to the next question.");

}, 4000);

});



Qualtrics.SurveyEngine.addOnUnload(function()

{

/*Place your JavaScript here to run when the page loads*/

});



This code displays an error message after 4 seconds if I stay on a question. However, if I advance quicker than 4 seconds, the timeout error message carries over and will appear in the middle of the countdown on the next page (e.g., If I advance after 3 seconds, the next question will pop up an error message when the countdown has only gone down by 1 second). Do you have advice on how to avoid this carry over? Thank you for your help!
Hi @iadams,

originally I thought I'll solve it using clearTimeout function easily. However, I didn't manage to make this function work in Qualtrics. In another compiler, the following code worked, but in Qualtrics not.



<button onclick="myStopFunction()">Stop the alert</button>

<script>

var myVar;

myVar = setTimeout(function(){ alert("Hello"); }, 3000);

function myStopFunction() {

clearTimeout(myVar);

}

</script>





So if some else won't manage to solve your problem, you could display your error message somewhere in the page instead of in the pop-up window:

1) Add following HTML element to the place where the future error message will be:` <p id="demo"> </p>` (note that there should be at least a space inside since Qaultrics removes empty elements)

2) Add folowing JavaScript to the question which contains the HTML element.



Qualtrics.SurveyEngine.addOnReady(function()

{

setTimeout(function(){ document.getElementById("demo").innerHTML = "ERROR"; }, 3000);

});



One more tip: In this verion, the user have unlimited time to answer the question after the error message appears. If you don't want this, you could use another timer containing `this.clickNextButton();` to autoadvance the page.

Leave a Reply