addEventListener.delay() 可以吗?

addEventListener.delay() can be doable?

你好,我有一个用 jquery 编写的 qualtrics 代码。我需要为事件监听实现延迟功能。我的代码启用按键作为输入。应该有延迟功能...

我应该在前 2000 毫秒内禁用按键,然后再启用它们。

我的任务有 3 个不同的部分,所有部分都根据时间显示或隐藏。

这是我的代码;

function disableMouse(event) {
  event.stopPropagation()
}
window.disableMouseFunction = disableMouse


Qualtrics.SurveyEngine.addOnload(function() {

  document.body.style.cursor = 'none'; // hide cursor

  setTimeout(function() {
    document.addEventListener('keydown', function keydownCallback(e) {
      var that = this;
      var aa = null;
      switch (e.keyCode) {

        case 37: // left arrow key                 
          that.setChoiceValue(1, true) //pic a     
          aa = 1;
          break;

        case 39: // right arrow key                  
          that.setChoiceValue(2, true) //pic B     
          aa = 1;
          break;
      }
      if (aa) {
        document.removeEventListener('keydown', keydownCallback, true);
        //move to the next page after delay
        that.clickNextButton();
      }
    });
  }, 1000);
});

Qualtrics.SurveyEngine.addOnReady(function() {
  setTimeout(function() {
    jQuery('#showfirst').delay(500).hide(1);
    jQuery('#hideafter').delay(500).show(1);
    jQuery('#hideafter').delay(2000).hide(1);
    jQuery('#reveallater').delay(2550).show(2);
  })
});

你必须听文档,因为 this 中根本没有 input...

我给你一个函数,试试看。

function disableSurveyArrows(delay){  // delay is in seconds
  var ArrowsDisabled = true;

  setTimeout(function(){
    ArrowsDisabled = false;
    $("#onoff").html("Arrows enabled now.")
  },5000);


  $(document).on("keydown",function(e){
    console.clear();
    if(ArrowsDisabled){
      console.log(".");
      return false;
    }
    if(e.which==37){
      console.log("Left");
    }
    if(e.which==39){
      console.log("Right");
    }
  });
}

// Call the function. 5 seconds is passed here.
disableSurveyArrows(5);
span{
  margin:calc(25% - 50px);
}
#onoff{
  font-size:4em;
  text-align:center;
  width:100%
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span style="font-size:100px">←</span><span style="font-size:100px">→</span>
<div id="onoff">Arrows disabled</div>

我无法在前 2 秒内禁用输入,所以我只是隐藏选项并让它们在 2 秒后出现。 People do not try to answer when the selections are not visible.不是完美的解决方案,但它有效。

<style> .QuestionBody { display: none;} </style> //in qualtrics

对于有类似问题的人来说,这可能是一个有用的技巧。