2017-08-30 123 views
1

我需要将currentQuest.options传递给showResult函数,而无需在这些函数之外声明全局var或var。jquery通过函数之间的var不使用全局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(); 
    }); 

的逻辑是:当按钮点击 - >启动计时器,示出了第一个问题(从池中随机选择) 当计时器命中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)]]; 
} 
+0

谢谢Ghasan Al-Sakkaf的建议。但是,这样做意味着问题不会在计时器达到0后随机生成。它仅在单击按钮时第一次运行。 – Viet

+0

@vietnguyen,请检查最新的答案。 –

+0

非常感谢Ghasan Al-Sakkaf!顺便问一下,你是否知道我怎样才能从池中生成一个随机问题,但使用过的问题不会再被选中? – Viet