jquery在函数之间传递var而不使用全局var或var outside函数

时间:2017-08-30 03:58:25

标签: jquery

我需要将currentQuest.options传递给showResult函数,而不在这些函数之外声明全局var或var。

有可能吗?我已经阅读了其他类似的问题,但所有正确的答案都是使用全局变量。

var pool = {
        q1: {
            question: "This is question number 1",
            options: ["Option 1", "Option 2", "Option 3", "Option 4"],
            answer: 0, //answer uses property option key
            used: false
        },
        q2: {
            /*...*/
        },
        /*...just more q here...*/
    }

    //choose a random question
    function randomQuest(obj) {
        var keys = Object.keys(obj);
        return obj[keys[keys.length * Math.random() << 0]];
    }

    //show question and options
    function showQuest() {
        var currentQuest = randomQuest(pool);
        $(".answer").html(""); //clear old answer
        $(".question").html(currentQuest.question);
        $(".options").html(currentQuest.options);
        currentQuest.used = true; //mark question used

        console.log(currentQuest.question);
        console.log("used? ", currentQuest.used);
    }

    function showResult() {
        $(".question").html("");
        $(".options").html("");
        var x = currentQuest.answer;
        $(".answer").html("Here is the answer " + currentQuest.options[x]); //how to pass currentQuest here?
    }

    function timer() {
        var seconds = 4;
        var interval = setInterval(function() {
            seconds--;
            $(".timer").html("<p>Time remaining: " + seconds + " s</p>");       
            if (seconds == 0) {
                clearInterval(interval);
                showResult();
            }   
        }, 1000);
    }

    //when click start game
    $(".btn").on("click", function() {
        timer();
        showQuest();
    });

逻辑是:点击按钮时 - &gt;启动计时器,显示第一个问题(从池中随机选择) 当计时器点击0,显示结果,等待几秒钟,重置计时器,显示另一个问题,再次启动计时器。

1 个答案:

答案 0 :(得分:3)

您可以使用函数参数来实现此目的。

function showQuest(currentQuest) {
    $(".answer").html(""); //clear old answer
    $(".question").html(currentQuest.question);
    $(".options").html(currentQuest.options);
    currentQuest.used = true; //mark question used

    console.log(currentQuest.question);
    console.log("used? ", currentQuest.used);
}

function showResult(currentQuest) {
    $(".question").html("");
    $(".options").html("");
    var x = currentQuest.answer;
    $(".answer").html("Here is the answer " + currentQuest.options[x]); //how to pass currentQuest here?
}

function timer(currentQuest) {
    var seconds = 4;
    var interval = setInterval(function() {
        seconds--;
        $(".timer").html("<p>Time remaining: " + seconds + " s</p>");       
        if (seconds == 0) {
            clearInterval(interval);
            showResult(currentQuest);
        }   
    }, 1000);
}

//when click start game
$(".btn").on("click", function() {
    var currentQuest = randomQuest(pool);
    timer(currentQuest);
    showQuest(currentQuest);
});

编辑:请尝试使用此方法获取随机索引。

// From MDN
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function randomQuest(obj) {
    var keys = Object.keys(obj);
    return obj[keys[getRandomInt(0, keys.length)]];
}