简体   繁体   English

为什么隐藏图像的功能无法正常工作?

[英]Why is my function to hide image not working properly?

My code was working properly until I decided to make a small change, and I guess I accidentally deleted something because my console is saying hide image is not defined at decrement when I already defined hide image. 我的代码工作正常,直到我决定进行一些小改动,我想我不小心删除了一些东西,因为我的控制台说当我已经定义了隐藏图像时,隐藏图像没有定义。 I can't find my error everything worked fine :'(. I went over my hide image function and it seems like everything is correct. When I load it on html the error seems to appear when a user does not make a selection is runs the function decrement, so when time reaches zero it displays an image with the correct answer, and it used to clear it out and display the next question with the available choices, but now it just stays on the if time = 0 screen and doesn't move on to the next question. 我找不到我的错误一切正常:'(。我查看了我的隐藏图像功能,似乎一切都是正确的。当我在html上加载时,错误似乎出现在用户没有进行选择时运行函数递减,所以当时间到达零时,它会显示一个具有正确答案的图像,并用它清除它并显示下一个带有可用选项的问题,但现在它只停留在if time = 0屏幕上并且不会继续下一个问题。

  $(document).ready(function () {
    //set up object-array for questions
    var trivia = [
        {
            question: "On Drake & Josh, what's Megan favorite phrase?'",
            choices: ["Boobz", "Idiots", "Oh, really?", "Damn! Where are my 
    apples?"],
            rightChoice: 0,
            image: "assets/images/boobs.gif",
            background: "<img src='assets/images/90back.jpg'>"
        },
        {
            question: "What color lipstick does Spongebob use when he kisses 
    Mr. Krabs fake Millionth dollar?",
            choices: ["Magenta", "Stardust", "Coral Blue #Oof", "Blorange"],
            rightChoice: 2,
            image: "assets/images/spongebob-coral-blue.gif",
            background: "<img src='assets/images/90cart.jpg'>"
        },
        {
            question: "What thottie accessory was popular in the 90's, that 
    is currently popular today?",
            choices: ["chokers", "bandaids", "airpods", "tidepods"],
            rightChoice: 0,
            image: "assets/images/chokers.gif",
            background: "<img src='assets/images/90back.jpg'>"
        },
        {
            question: "During sleepovers, Mystery Date allowed girls to date 
    which sexy actor?",
            choices: ["Port", "James Franco", "Paul Rudd", "Chris Evans, Mr. 
    America"],
            rightChoice: 3,
            image: "assets/images/chris-evans.gif",
            background: "<img src='assets/images/90cart.jpg'>"
        },
        {
            question: "What was the SPICIEST band in the 90's?",
            choices: ["Madonna", "Hillary Clinton", "BackStreet Boyz", "The 
    Spice Girls"],
            rightChoice: 3,
            image: "assets/images/zig-a-zig-ha.gif",
            background: "<img src='assets/images/90back.jpg'>"
        }
    ];
    var rightAnswer = 0;
    var wrongAnswer = 0;
    var unansweredCount = 0;
    var time = 15;
    var intervalId;
    var userSelection = "";
    var selected = false;
    var running = false;
    var totalCount = trivia.length;
    var chosenOne;
    var triviaRand;
    var newArray = [];
    var placeHolder = [];

    //hide resetBtn until called
    $("#resetBtn").hide();
    //click startBtn button to start game
    $("#startBtn").on("click", function () {
        $(this).hide();

        displayTrivia();
        runTime();
        for (var i = 0; i < trivia.length; i++) {
            placeHolder.push(trivia[i]);
        };

    })
    //time: run
    function runTime() {
        if (!running) {
            intervalId = setInterval(decrement, 1000);
            running = true;
        }
    }
    //time--
    function decrement() {
        $("#timeLeft").html("<h4>👻 Madonna, we're running out of time 👻 " 
    + time + " 👀</h4>");
        time--;

        //stop time if reach 0
        if (time === 0) {
            unansweredCount++;
            stop();
            $("#choicesDiv").html("<p>Oh no! You ran out of time 😂. The 
    correct choice is: " + chosenOne.choices[chosenOne.rightChoice] + " 
   </p>");
            hideimage();
        }
    }

    //time stop
    function stop() {
        running = false;
        clearInterval(intervalId);
    }
   play question and loop though and display possible answers
    function displayTrivia() {
        //generate random triviaRand in array
        triviaRand = Math.floor(Math.random() * trivia.length);
        //console.log(triviaRand);
        chosenOne = trivia[triviaRand];
        console.log(chosenOne);

        $("#questionDiv").html("<h2>" + chosenOne.question + "</h2>");
        for (var i = 0; i < chosenOne.choices.length; i++) {
            var newUserChoice = $("<div>");
            newUserChoice.addClass("answerChoices");
            newUserChoice.html(chosenOne.choices[i]);
            //assign array position to it so can check rightChoice
            newUserChoice.attr("userChoices", i);
            $("#choicesDiv").append(newUserChoice);
        }

        //click function to select rightChoice
        $(".answerChoices").click(function () {
            //parseInt() function parses a string argument and returns an 
    integer of the specified radix
            //locate array based on userChoice
            userSelection = parseInt($(this).attr("userChoices"));
            console.log(userSelection);
            if (userSelection === chosenOne.rightChoice) {
                console.log(chosenOne.choices[chosenOne.rightChoice]);
                stop();
                selected = true;
                rightAnswer++;
                userSelection = "";
                $("#choicesDiv").html("<p>Damn, boi 🐱‍🐉👌</p>");
                hideimage();
                console.log(rightAnswer);
            } else {
                stop();
                selected = true;
                wrongAnswer++;
                userSelection = "";
                $("#choicesDiv").html("<p>🤔That is incorrect! The correct 
    choice is: " + chosenOne.choices[chosenOne.rightChoice] + "</p>");
                hideimage();
                console.log(wrongAnswer);
            }
        })

        function hideimage() {
            $("#choicesDiv").append("<img src=" + chosenOne.image + ">");
            newArray.push(chosenOne);
            trivia.splice(triviaRand, 1);

            var hideimg = setTimeout(function () {
                $("#choicesDiv").empty();
                time = 15;

                //run the score screen if all questions answered
                if ((wrongAnswer + rightAnswer + unansweredCount) === 
    totalCount) {
                    //clearbck();
                    $("#questionDiv").empty();
                    $("#questionDiv").html("<h3>🧐 Game Over!  Let's see 
    your score 😱: </h3>");
                    $("#choicesDiv").append("<h4> 🤪 Correct: " + 
    rightAnswer + "</h4>");
                    $("#choicesDiv").append("<h4> 🤬 Incorrect: " + 
    wrongAnswer + "</h4>");
                    $("#choicesDiv").append("<h4> 🤯 Unanswered: " + 
    unansweredCount + "</h4>");
                    $("#resetBtn").show();
                    rightAnswer = 0;
                    wrongAnswer = 0;
                    unansweredCount = 0;

                } else {
                    runTime();
                    displayTrivia();

                }
            }, 2000);


        }

        $("#resetBtn").on("click", function () {
            $(this).hide();
            $("#choicesDiv").empty();
            $("#questionDiv").empty();
            for (var i = 0; i < placeHolder.length; i++) {
                trivia.push(placeHolder[i]);
            }
            runTime();
            displayTrivia();

        })

    }
    })` 

Just as a syntax error correction! 就像语法错误纠正一样! You should use single or double quotation in src attribute of img tag in hideimage function: 你应该在hideimage函数中使用img标签的src属性中的单引号或双引号:

$("#choicesDiv").append("<img src=' " + chosenOne.image + " '>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM