【问题标题】:I would like to delay something after it's been clicked我想在点击后延迟某些内容
【发布时间】:2018-03-22 17:07:59
【问题描述】:

我正在创建一个多项选择题程序,并且我拥有它,因此一旦选择了正确答案,就会生成一个新问题。

虽然我希望它自动转到下一个问题,但我希望它延迟大约 0.5 秒,以便用户可以看到他们的答案是正确的。

我选择删除该类并将其替换为另一个类,以便背景改变颜色。一旦出现新问题,我希望所有颜色恢复正常,因此我再次删除新类并用旧类替换它。

如果我不自动进入新问题,颜色会以我喜欢的方式出现,但如果我创建它以使其自动移动,我无法保持显示不变。

在通过论坛搜索后,我读到 setTimeout 应该可以工作,但我没有取得太大的成功。我也尝试过制作动画,这需要时间,但这对我也不起作用。动画效果很好,但它仍然继续处理新的问题。

我将包含整个程序,因为它可能会更好,但我正在处理的部分位于函数 check() 下。

长期以来,我一直在试图弄清楚如何延迟某些事情,但完全没有成功。哦,请对我好一点,我的经验很少。我只是通过可汗学院课程学习了如何使用 javascript。谢谢!

        <!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Project: listening to sounds </title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

        <!-- ***************** CSS styles ***************** -->
        <style>
            body {
                font-family: comic sans ms, sans-serif;
                background-image: url("background.jpg");
                background-color:  rgb(216, 252, 252);

            }
            form {
                font-size: 1.2em;
            }

            #text {
                background-color: wheat;
                width: 150px;
                height: 25px;
                color: blue;
                font-size: 1em;

            }

            .SoundBite {
                float: left;
                clear: none;
                position: absolute;
                top: 130px;
                left: 100px;
                padding: 5px;
                background: darkblue;
                color: white;
                height: 120px;
                width: 200px;
                border-radius: 50px;
                margin: auto;
                text-align: center;
                vertical-align: middle;
                font-size: 5em;
            }

            .SoundBite:hover {
                background-color: darkgreen;
                cursor: pointer;
            }

            .Score {
                float: right;
                clear: none;
                position: absolute;
                top: 50px;
                left: 140px;
                padding: 5px;
                background: darkblue;
                border-color: pink;
                border: 5px;
                opacity: 0.8;
                color: white;
                height: 45px;
                width: 120px;
                border-radius: 50px;
                margin: auto;
                text-align: center;
                vertical-align: middle;
                font-size: 2em;
                pointer-events: none;
            }           

            .Answer {
                position: absolute;
                color: white;
                height: 45px;
                width: 120px;
                padding: 5px;
                border-radius: 25px;
                margin: auto;
                text-align: center;
                vertical-align: middle;
                font-size: 2em;
                background-color: darkblue;
            }

            .AnswerCorrect {
                position: absolute;
                color: white;
                height: 45px;
                width: 120px;
                padding: 5px;
                border-radius: 25px;
                margin: auto;
                text-align: center;
                vertical-align: middle;
                font-size: 2em;
                background-color: green;            
            }

            .Answer:hover {
                background-color: #e44404;
                cursor: pointer; 

            }

            .AnswerWrong {
                position: absolute;
                color: white;
                height: 45px;
                width: 120px;
                padding: 5px;
                border-radius: 25px;
                margin: auto;
                text-align: center;
                vertical-align: middle;
                font-size: 2em;
                background-color: red;
            }

            #Answer1 {
                top: 20px;
                left: 400px;

            }
            #Answer2 {
                top: 85px;
                left: 400px;

            }
            #Answer3 {
                top: 150px;
                left: 400px;
            }
            #Answer4 {
                top: 220px;
                left: 400px;
            }

            #Answer5 {
                top: 290px;
                left: 400px;
            }


        </style>
    </head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>


    <body>

    <audio id="audio" src="audio/rug.mp3" autostart="false" ></audio>
    <audio id = "win" src="audio/win.mp3" autostart = "true"></audio>
    <audio id = "lose" src = "audio/lose.mp3" autstart = "true"></audio>

    <a onclick="playSound();"><div span class="SoundBite"><i class="fa fa-file-sound-o" id="audio" src="audio/rug.mp3" autostart="false" style="font-size:64px;color:skyblue"></i></div></a>
    <a onclick ="checkAnswer = 1; check();"><div class = "Answer" id="Answer1">1</div>
    <a onclick ="checkAnswer = 2; check();"><div class = "Answer" id="Answer2">2</div>
    <a onclick ="checkAnswer = 3; check();"><div class = "Answer" id="Answer3">3</div>
    <a onclick ="checkAnswer = 4; check();"><div class = "Answer" id="Answer4">4</div>
    <a onclick ="checkAnswer = 5; check();"><div class = "Answer" id="Answer5">5</div>
    <div class = "Score">Score</div>


    <!-- <a onclick ="next();"><div id = "Next">Start</div> -->



    <!-- ********************************* Javascript programming follows ********************************* -->

    <script>
    $(function() {
    });

        // declare the variables to be used ... do I need global variables? Maybe I should think about these...
        var word, ext, directory, wordPosition, decoyWordPosition, answerPosition, decoyAnswerPosition, answer, checkAnswer, correct, incorrect, tries;
        directory = "audio/";
        ext = ".mp3";
        correct = 0;
        incorrect = 0;
        tries = 0;



        // list of words that are spoken
        word = ["dam", "dog", "dug", "cat", "cot", "cut", "ran", "rot", "rug"]; 
        wordPosition = Math.floor(Math.random()*word.length); // returns a random array wordPosition


    /***************************************************************************
     ** Functions:
     **
     **************************************************************************/


    // function to display word
    function displayWord() {
        //$(".SoundBite").text(word[wordPosition]);
        //$("#audio").attr("src", directory+word[wordPosition]+ext);
    }

    // function to display the answer in one of the positions that are assigned
    function displayAnswer(answerNumber, wordNumber) {
        $("#Answer"+answerNumber).text(word[wordNumber]);
        $("#audio").attr("src", directory+word[wordPosition]+ext);
    }   

    // function to play the sound 
    function playSound() {        
          var sound = document.getElementById("audio");
          sound.play();
      }

    // function to play a winning sound
    function win() {
        var sound = document.getElementById("win");
        sound.play();
    }

    // function to play a losing sound
    function lose() {
        var sound = document.getElementById("lose");
        sound.play();
    }

    function timer() {
        new Date().toLocaleTimeString();
    }

    function check() {
            if (answer == checkAnswer) {
                //$("#Answer"+checkAnswer).animate({height: "45px", opacity: '0'})
                                        //.animate({height: "45px", opacity: '1.0'});
                $("#Answer"+checkAnswer).animate({height: "45px", opacity: '0'})
                                        .animate({height: "45px", opacity: '1.0'})
                                        .removeClass("Answer").addClass("AnswerCorrect");
                win();
                correct++;
                tries++;
                refresh();

                next();
                // need a delay function -- can't get it to work.


            } else {
                $("#Answer"+checkAnswer).removeClass("Answer").addClass("AnswerWrong"); //css({'background-color': 'red'});
                lose();
                incorrect++;
                tries++;

            }

            // write in the score
            $(".Score").text(Math.round(correct/tries*100)+"%");
    }

    // making a function to populate the answers 
    function populateAnswers() {
    for (var i = 0; i < 6; i++) {
        if (answerPosition < 6) {
            if (decoyWordPosition == wordPosition) {
                answer=answerPosition
            }
            displayAnswer(answerPosition, decoyWordPosition);
            answerPosition++;
            decoyWordPosition++;
            if (decoyWordPosition >= word.length) { // want to make sure that the words are within the array
                decoyWordPosition = 1               // reset to the beginning to 'wrap' the array.
                }

            } else {
                answerPosition = 1;
            }
        }
    }

    // using a random generator to place the answer in a random spot 1 through 4
    function randomGenerator() {
    answerPosition = Math.floor(Math.random()*4)+1;
    }

    // returns a random array wordPosition
    function randomWord() {
        wordPosition = Math.floor(Math.random()*word.length); 
    }

    function refresh() {
        for (var i = 1; i <= 5; i++) {
        $("#Answer"+i).removeClass("AnswerWrong").addClass("Answer");
        $("#Answer"+i).removeClass("AnswerCorrect").addClass("Answer");
        }
    } 

    /***************************************************************************
     *  program   as a function                                                             *
     **************************************************************************/

    function next() {

        $("#Next").text("Continue");

        randomWord();
        randomGenerator();

        // make the decoy answers randomly
        if (wordPosition == 0 || wordPosition == 1) { 

            // In case the array is at the beginning: make the decoy start at the same spot as the wordPosition
            decoyWordPosition = wordPosition;
            } else {
                // start the decoy word after the word
                decoyWordPosition = wordPosition -2;
        }

        populateAnswers();

    }
    next();

    </script>
    </body>
    </html>

【问题讨论】:

  • 不需要粘贴整个HTML文档,感兴趣的部分就可以了:)。我们可以看看你是如何实现你的 setTimeout 函数的吗?您的问题可能很简单,例如语法错误或更复杂。
  • 感谢您的回复!对不起,我发布了所有内容。我只是不确定我的程序中是否有其他问题导致了问题,所以我只是展示了所有内容(以防万一)。老实说,我不记得我以前尝试过什么,因为我尝试了很多不同的方法。我想我可能做过这样的事情:'setTimeout(refresh(), 1000);设置超时(下一个(),1000);'刷新和下一个功能正常工作,根本不暂停。我尝试使用警报(确实会暂停),所以我真的不明白我做错了什么。
  • 啊哈!我发现我做错了什么!我在函数后面加了括号,但我不应该加。
  • 看起来这是一个简单的语法错误,毕竟 :) 很高兴你自己发现了它
  • 谢谢麦克。是的。就像我之前所说的,我对这一切真的很陌生,所以我并没有真正理解语法。我注意到如果我在函数周围使用引号,那也可以。

标签: jquery-animate settimeout delay


【解决方案1】:

仅适用于试图找到 setTimeout 功能解决方案的其他人。

我是这样使用它的:

setTimeout(myFunction(), 2000);

但是,它不适用于函数后面的括号。您需要省略这些括号:

setTimeout(myFunction, 2000);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-28
    • 2016-09-07
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多