using space to advance - autoadvance problem | XM Community
Skip to main content
Solved

using space to advance - autoadvance problem

  • 3 July 2024
  • 2 replies
  • 23 views

Hi!

For my survey in Qualtrics, I have the following design:

  1. A word is shown on the screen (text/graphic question)
  2. at a certain point, participants need press “space” to advance to the next question (text entry), the advance button is hidden.
  3. Upon the same space press, the reaction time is registered (the time the word was shown on the screen until space was pressed)
  4. On the next page, the normal advance button is used instead of the space bar (so space shouldn’t work)
  5. On the next page, the cycle restarts and a new word is shown on the screen

I have tried the following code for the spacebar press (advancing) and the reaction times:

Qualtrics.SurveyEngine.addOnload(function () {
    
 let timeOnLoad = new Date().getTime();
 var that = this;
 Event.observe(document, 'keydown', function keydownCallback(e) {
  var choiceID = e.keyCode;
  if (choiceID) {
   let RT = new Date().getTime() - timeOnLoad;
    Qualtrics.SurveyEngine.setEmbeddedData("RT1", RT);
   console.log(RT);
   Event.stopObserving(document, 'keydown', keydownCallback);
   that.setChoiceValue(choiceID, true);
  }
 });
});

Qualtrics.SurveyEngine.addOnReady(function() {
    $('NextButton').hide();
    if($('PreviousButton')) $('PreviousButton').hide();
    var evt = document.on('keydown', function(e) {
        if (e.which == 32) {  //space bar pressed
            evt.stop();
            $('NextButton').click();
        }   
    });
});

As you can see, I use embedded data for the reaction times (but perhaps I could also use the timing question). Anyway, it all works, so the reaction times are measured upon space press and the page is forwarded. The only problem is the survey starts autoadvancing after the first space press if I leave the next questions open for a while. Even the questions that do not have the code for the space press in them start autoadvancing. How can I solve this?

 

Thanks!

2 replies

Userlevel 7
Badge +22

In your code, you appear to be adding multiple event listeners.

Please try the one below:

Qualtrics.SurveyEngine.addOnload(function () {
this.hideNextButton();
});
Qualtrics.SurveyEngine.addOnReady(function () {
const quest = this;
const qc = quest.getQuestionContainer();
const startTime = Date.now();
const handleKeyPress = function (e) {
if (e.keyCode == 32) {
const reactionTime = Date.now() - startTime;
Qualtrics.SurveyEngine.setEmbeddedData("RT1", reactionTime);
document.removeEventListener("keydown", handleKeyPress);
qc.style.display = "none";
quest.clickNextButton();
}
};

document.addEventListener("keydown", handleKeyPress, false);
});

 

Badge +3

great, that worked! Thanks!!

Leave a Reply