How to Delay Answers with a KeyPress Javascript | XM Community
Skip to main content

In my survey, participants choose between two pictures. I used javascript so that the participants can answer using either “E” or “I” on their keyboard. I want to make sure that the participants do not answer before the pictures have had time to fully load. Is there a javascript I can use to prevent the participants from advancing to the next question for a set amount of time? Ideally it would be a delay at least .75 seconds, and unfortunately I can’t use the timing question type because there is no submit button. Thank you!!

 

Here’s the javascript I used for the keypress:

Qualtrics.SurveyEngine.addOnload(function()
{
this.hideNextButton();
this.hidePreviousButton();
var that = this;
Event.observe(document, 'keydown', function keydownCallback(e) {
  var choiceID = null;
  switch (e.keyCode) {
    case 69: // 'e' was pressed
      choiceID = 1;
      break;
    case 73: // 'i' was pressed
      choiceID = 2;
      break;
  }
  if (choiceID) {
    Event.stopObserving(document, 'keydown', keydownCallback);
    that.setChoiceValue(choiceID, true);
    that.clickNextButton();
  }
});
});

Qualtrics.SurveyEngine.addOnload(function () {
this.hideNextButton();
this.hidePreviousButton();
var that = this;

setTimeout(() => {
Event.observe(document, "keydown", function keydownCallback(e) {
var choiceID = null;
switch (e.keyCode) {
case 69: // 'e' was pressed
choiceID = 1;
break;
case 73: // 'i' was pressed
choiceID = 2;
break;
}
if (choiceID) {
Event.stopObserving(document, "keydown", keydownCallback);
that.setChoiceValue(choiceID, true);
that.clickNextButton();
}
});
// Change this value. It is in ms
}, 750);
});

 


Leave a Reply