When I hit the submit button without answering the questions, it restarts the time again (so even if I hit the button at 20 seconds it restarts again from 45 seconds). However, the code works fine if I don't submit an answer within 45 seconds it then moves to next question.:
Attached code:
Qualtrics.SurveyEngine.addOnload(function() {
var questionId = this.getQuestionInfo().QuestionID;
console.log(`Setting up question ${questionId}...`);
// Hide the "No Response" option by applying CSS
var css = `
#QID8-5-label {
display: none !important;
visibility: hidden !important;
}
input#QR~QID8~5 {
display: none !important;
visibility: hidden !important;
pointer-events: none !important;
}
labelsfor="QR~QID8~5"] {
display: none !important;
visibility: hidden !important;
}
`;
jQuery("head").append("<style>" + css + "</style>");
console.log("CSS to hide 'No Response' option applied.");
// Track if the timer is cleared or if the question has moved forward
var timerCleared = false;
var questionCompleted = false;
// Function to move to the next question
function moveToNextQuestion() {
if (!questionCompleted) {
questionCompleted = true;
console.log(`Moving to the next question from question ${questionId}.`);
jQuery("#NextButton").click();
}
}
// Start the timer for 45 seconds
var timer = setTimeout(function() {
if (!timerCleared) {
var selected = jQuery("#" + questionId + " input=type='radio']:checked").length > 0;
if (!selected) {
console.log(`No option selected in question ${questionId}, moving to the next question.`);
jQuery("#QID8-5-label").click(); // Optional click on "No Response"
moveToNextQuestion();
}
}
}, 45000); // 45 seconds
console.log(`Timer started for 45 seconds for question ${questionId}.`);
// Listen for any selection to clear the timer if an option is selected
jQuery("#" + questionId + " input type='radio']").on('change', function() {
if (!timerCleared) {
clearTimeout(timer); // Clear the timer if an option is selected
timerCleared = true; // Ensure the timer is not restarted
console.log(`Timer cleared due to selection in question ${questionId}.`);
}
});
// Cleanup event listeners and timer on question unload
Qualtrics.SurveyEngine.addOnUnload(function() {
clearTimeout(timer); // Ensure the timer is cleared on unload
jQuery("#" + questionId + " inputetype='radio']").off('change');
console.log(`Cleaned up for question ${questionId}.`);
});
});