Javascript: Using Enter to advance after a delay | XM Community
Solved

Javascript: Using Enter to advance after a delay

  • 7 October 2019
  • 3 replies
  • 36 views

I've been using the following code from a gist to allow the use of the 'enter' key to advance to the next question in addition to the next button:

```
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
/*Advance on enter - modified from the following gist: */
/*https://gist.github.com/mattbloomfield/de3a20a5180c274f79e7dfcaeb35ea66*/
var qid = this.questionId;
document.onkeydown = function(event) {
console.log('keydown',event);
if (event.which == 13) {
event.preventDefault();
jQuery('#NextButton').click();
}
}
});
```

I want to delay the page advance for 3 seconds, which I'm currently doing through a timing question and enabling submit after 3 seconds.
However, I've noticed while testing that hitting the enter key can jump ahead before the 3 seconds are finished (when the next button is displayed). Does anyone know what the right way to incorporate this delay into the Javascript?
icon

Best answer by fleb 9 October 2019, 09:33

View original

3 replies

Userlevel 5
Badge +6
Hi ,
I'd not use Qualtrics timing, but design my own timing in JS. You can use the `setTimeout() ` method.
Thank you. I'll look into using that as an option and see if I can get the behavior I'm looking for.
Okay. I managed to get the behavior I wanted with a bit of working with setTimeout. For reference to anyone in the future, this worked:
```
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
$('NextButton').disable();
setTimeout(function() { jQuery("#NextButton").enable();

},3000);

/*Advance on enter - modified from the following gist: */
/*https://gist.github.com/mattbloomfield/de3a20a5180c274f79e7dfcaeb35ea66*/
var qid = this.questionId;

setTimeout(() => { document.onkeydown = function(event) {
console.log('keydown',event);
if (event.which == 13) {
event.preventDefault();
jQuery('#NextButton').click();
}
}}, 3000);

});
```

Leave a Reply