当前位置: 首页 > 知识库问答 >
问题:

在setInterval函数Console.log中的click事件上double(有时是三重)click事件[duplicate]

锺离晗昱
2023-03-14

第一次问问题,请耐心听我说。非常感谢你的帮助!

我的代码如下所示。我正在尝试创建一个琐事游戏。

//  To set global variables and objects. 
var game = [
  [
    "How did Daenarys Targaryen eventually hatch her dragon eggs?", ["1. In a lightning storm", "2. In a funeral pyre", "3. In a fireplace", "4. In a frozen cave"],
    "2. In a funeral pyre",
    "assets/images/q1",
  ],
  [
    "The phrase 'Valar Morghulis' or 'all men must die' is usually responded with:", ["1. Valar Dohaeris or 'all men must serve'", "2. Valar Rohnas or 'all men must live'", "3. Valar GoGo or 'all men must dance'", "4. Valar Morghulis or 'all men must fight'"],
    "1. Valar Dohaeris or 'all men must serve'",
    "assets/images/q2",
  ],
  [
    "What is the only thing that can put out volatile Wildfire?", ["1. Sand", "2. Water", "3. Dragon's blood", "4. Sunlight"],
    "1. Sand",
    "assets/images/q3",
  ],
  [
    "Besides dragonglass, what is the only other substance capable of defeating White Walkers?", ["1. Weirwood", "2. Wildfire", "3. Valyrian Steel", "4. Magma"],
    "3. Valyrian Steel",
    "assets/images/q4",
  ],
  [
    "Arya's punishment for stealing from the Many-Face God is:", ["1. Death", "2. Memory Loss", "3. Blindness", "4. A Broken Arm"],
    "3. Blindness",
    "assets/images/q5",
  ],
  [
    "What was the name of Ned Stark's greatsword?", ["1. Oathkeeper", "2. Deathbringer", "3. Northguard", "4. Ice"],
    "4. Ice",
    "assets/images/q6",
  ],
  [
    "Who shoots the flaming arrow that subsequently destroy's Stannis' fleet in Blackwater Bay?", ["1. Tyrion Lannister", "2. King Joffrey", "3. Jaime Lannister", "4. Bronn"],
    "4. Bronn",
    "assets/images/q7",
  ],
  [
    "Prince Oberyn Martell is nicknamed the 'Red Viper' because of his combat and:", ["1. Pride in drawing blood first", "2. Knowledge of poisons", "3. Nighttime attacks", "4. Ruby-colored armor"],
    "2. Knowledge of poisons",
    "assets/images/q8",
  ],
  [
    "The Night King was created using a dagger made of:", ["1. Blue Ice", "2. Valyrian Steel", "3. Dragonglass", "4. Obsidian"],
    "3. Dragonglass",
    "assets/images/q9",
  ],
  [
    "How many arrows does Ramsay Bolton let loose at Rickon Stark?", ["1. Three", "2. Four", "3. Five", "4. Six"],
    "2. Four",
    "assets/images/q10",
  ],
]

var b = $.extend(true, [], game);
var qCountdown;
var ans;
var counter;

$(document).ready(function() {
  var correct = 0;
  var incorrect = 0;
  var unanswered = 0;
  var button = $(".optBtn");
  var start = $("#startBtn");
  var question = $("#question");
  var answer = $("#answer");
  var countdown = $("#timeout");
  var options;
  var qCounter = 0;

  // To hide certain HTML elements before the game starts.
  button.hide();

  // To Initiate the newGame function.
  function newGame() {
    nextQuestion();
  }

  // To initiate the nextQuestion function. 
  function nextQuestion() {
    countdown.show();
    question.show();
    button.show();
    start.hide();
    answer.empty();

    // Set the ending condition to display the results page. 
    if (qCounter !== 10) {
      test()
      // Set a condition and display for if the counter reaches 0.
      counter = 10;
      qCountdown = setInterval(function() {
        counter--;
        countdown.text(counter);
        if (counter === 0) {
          b.shift();
          clearInterval(qCountdown);
          countdown.hide();
          button.hide();
          console.log(qCounter + "TimeUp");
          console.log(b.length + "TimeUp");
          answer.text("The correct answer is " + ans + ".");
          $("#unanswered").text("Uh oh, you did not answer the question in time.");
          unanswered++;
          qCounter++;
          window.setTimeout(nextQuestion, 2000);
          return;
        }
      }, 1000);
    } else {
      b = $.extend(true, [], game);
      countdown.hide();
      question.hide();
      button.hide();
      start.show();
      clearInterval(qCountdown);
      answer.text("You guessed " + correct + " questions correctly, and " + incorrect + " questions incorrectly.");
      var unans = $("#unanswered");
      unans.text("You did not answer " + unanswered + " questions.");
      return;
    };
    var quest = question.text(b[0][0]);
    ans = b[0][2];
    var img;
    // console.log();

    // To assign each option button with one of the four options.
    for (i = 0; i < 4; i++) {
      btns = {
        generator: function() {
          return b[0][1][i];
        }
      };

      button.each(function() {
        $(this).text(btns.generator());
        $(this).attr("data-value", btns.generator());
        i++;
      });
    }
  };

  function test() {
    // On-click function for the buttons to register a correct or incorrect guess. 
    button.on("click", function() {
      button.hide();
      console.log("You clicked a button once");

      options = $(this).attr("data-value");
      countdown.hide();
      question.hide();
      clearInterval(qCountdown);
      window.setTimeout(nextQuestion, 2000);
      qCounter++;
      b.shift();
      if (options === ans) {
        console.log(qCounter + "Click");
        console.log(b.length + "Click");
        answer.text("You guessed correctly, the answer is " + ans);
        correct++;
        button.off("click");
        return;
      } else {
        console.log(counter + "Clack");
        console.log(b.length + "Clack");
        answer.text("You guessed incorrectly, the answer is " + ans);
        incorrect++;
        button.off("click");
      };
    });

  }

  $("#startBtn").on("click", function() {

    // game = $.extend(true, [], b);
    newGame();
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

共有1个答案

丁嘉
2023-03-14

我认为问题是,当时间用完时,您调用nextQuestion(),它调用test(),它将.on('click')分配给类.optbtn的每个按钮。但是,只有在选择了答案时才会关闭click事件。

因此,如果时间用完而没有点击,那么每个按钮都会添加一个新的点击事件,这样就会有多个点击事件附加到按钮上。

尝试将按钮.关闭(“单击”);语句从它们在代码中的当前位置移动到NextQuestion()Test()顶部附近的单个调用。因此,每次计时器耗尽时,它将清除click事件,并附加它。

可能有更好的方法,而且这是一个很长的代码段,但是尝试一下那个建议。

 类似资料:
  • 该事件发生在用户单击CommandBarButton 对象时。 Private Sub CommandBarButton_Click (ByVal Ctrl As CommandBarButton, ByVal CancelDefault As Boolean) Click 事件的语法中包含两个参数,下表中列出了对这两个参数的说明。参数说明CtrlCommandBarButton 类型,必需。指示

  • 问题内容: 我在Netbeans中的Java中创建了一个表,并在其中填充了一些数据。现在,我想在单击该单元格时在与行中的特定列相对应的文本区域中显示一些详细信息。如何使用事件侦听器找出单元用户已单击的位置。 问题答案: 找到点击事件的位置并获取要搜索的单元格:

  • 我有一个输入字段,在那里我试图提出自动完成建议。代码看起来像 在输入的事件中,我想隐藏结果的div: 当我写一些东西到我的输入我发送请求到服务器并得到json响应,解析成ul- 当我点击这个解析的li元素时,我想将值从li设置为input并隐藏div 一切都很好,但是当我单击我的li事件在单击()之前触发,并且输入的值不会得到li的html。 如何在之前设置事件?

  • 问题内容: 我目前正在尝试编写一些JavaScript,以获取已单击的类的属性。我知道要以正确的方式执行此操作,应该使用事件侦听器。我的代码如下: 我希望每次单击其中一个类来告诉我该属性时,都会得到一个警告框,但是不幸的是,这不起作用。有人可以帮忙吗? ( 注 - 我可以很容易地做到这一点,但我 不 喜欢使用它) 问题答案: 这应该工作。返回与条件相匹配的元素的数组(类似于 数组 )(请参阅编辑)

  • 问题内容: 我正在尝试 在React元素上模拟,但无法弄清楚为什么它不起作用(触发时它没有反应)。 我只想使用JavaScript发布Facebook评论,但我停留在第一步(执行on 元素)。 我的代码是: 这是我尝试执行的网址: **https://www.facebook.com/plugins/feedback.php …** PS:我对React没有经验,我也不知道在技术上是否可行。这是可

  • 我目前正在尝试编写一些JavaScript来获取已单击的类的属性。我知道要正确地执行此操作,我应该使用事件监听器。我的代码如下: 我希望每次点击其中一个类时都有一个警告框来告诉我该属性,但不幸的是,这并不起作用。有人能帮忙吗?