【问题标题】:Two sounds playing at once in if statement in javascript在javascript中的if语句中同时播放两个声音
【发布时间】:2020-05-21 10:18:46
【问题描述】:

我正在制作一个测验网站,我目前正在编写一个函数来确定给定答案是否正确。我有两种不同的声音,一种是正确的,一种是错误的,有时它们会同时播放。例如,如果我的第一个问题正确,它只会播放正确的声音,但如果我的第二个问题不正确,它会同时播放两个问题,然后继续播放剩下的 3 个问题。我对 javascript 比较陌生,不知道为什么要这样做。这是我遇到问题的代码:

function correctOrIncorrect() {
    score = 0;
    var audioCorrect = new Audio('assets/audio/correct.mp3');
    var audioIncorrect = new Audio('assets/audio/incorrect.mp3');

    if (amount == 5 ) {
        if (document.querySelector('input[name="answer-1"]:checked').value === myQuestions[0].correct_answer) {
            score++;
            audioCorrect.play();
        } else {
            audioIncorrect.play();
        }
        if (document.querySelector('input[name="answer-2"]:checked').value === myQuestions[1].correct_answer) {
            score++;
            audioCorrect.play();
        } else {
            audioIncorrect.play();
        }
        if (document.querySelector('input[name="answer-3"]:checked').value === myQuestions[2].correct_answer) {
            score++;
            audioCorrect.play();
        } else {
            audioIncorrect.play();
        }
        if (document.querySelector('input[name="answer-4"]:checked').value === myQuestions[3].correct_answer) {
            score++;
            audioCorrect.play();
        } else {
            audioIncorrect.play();
        }
        if (document.querySelector('input[name="answer-5"]:checked').value === myQuestions[4].correct_answer) {
            score++;
            audioCorrect.play();
        } else {
            audioIncorrect.play();
        }
    }

非常感谢您的任何帮助,并提前感谢您。

【问题讨论】:

    标签: javascript


    【解决方案1】:

    每次调用correctOrIncorrect 函数时,它都会再次检查所有答案。 所以它会为所有以前和当前的答案产生声音。

    你应该通过例如。函数correctOrIncorrect 中的问题id 并仅检查该问题的答案的结果。

    类似

    var score = 0;
    function correctOrIncorrect(questionId) {
        var audioCorrect = new Audio('assets/audio/correct.mp3');
        var audioIncorrect = new Audio('assets/audio/incorrect.mp3');
    
        if (amount == 5 ) {
            if (document.querySelector('input[name="answer-'+questionId+'"]:checked').value === myQuestions[questionId].correct_answer) {
                score++;
                audioCorrect.play();
            } else {
                audioIncorrect.play();
            }
        }
    
    

    并将分数移出功能,以便您仍然可以正确保持分数。我不确定你用amount 做什么。

    【讨论】:

      【解决方案2】:

      那是因为如果您以您的方式回答 1 不正确和 2 正确,那么它会同时对 1 和对 2 正确。而且您正在同时检查所有答案。

      如果所有答案都正确,您可以播放正确的音调,否则,您可以播放错误的音调:

      var audioCorrect = new Audio('assets/audio/correct.mp3');
      var audioIncorrect = new Audio('assets/audio/incorrect.mp3');
      
      function correctOrIncorrect() {
          score = 0;
          if (amount == 5) {
              for (i = 0; i < amount; i++) {
                 if (document.querySelector('input[name="answer-' + (i + 1) + '"]:checked').value === myQuestions[i].correct_answer) {
                    score++;
                 }
              }
              if (score >= amount) {
                  audioCorrect.play();
              } else {
                  audioIncorrect.play();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-20
        • 2011-08-10
        • 2017-12-04
        • 1970-01-01
        • 1970-01-01
        • 2012-12-13
        • 2014-10-20
        相关资源
        最近更新 更多