【问题标题】:how to create a quiz countdown timer for each question如何为每个问题创建测验倒计时
【发布时间】:2020-05-14 21:16:00
【问题描述】:

我想为测验创建一个倒计时计时器,该计时器将在下一个问题时重置,并且如果时间用完并且玩家没有选择答案,也会转到下一个问题。谁能帮我创建这个plz?

var counter = 10;

setInterval(function () {
  counter--;

  if (counter >= 0) {
    id = document.getElementById('count');
    id.innerHTML = counter;
  }
  if (counter === 0) {
    id.innerHTML = 'Times Up!';
  }
}, 1000);
'''

【问题讨论】:

  • 有什么问题?这是做什么的?

标签: javascript timer settimeout countdown countdowntimer


【解决方案1】:

这是您的基本工作代码。玩弄它以获得您想要的结果。编码愉快!

var counter = 10;             //Time counter
var questionsCount = 0;       //Questions counter

//Questions array
var questions = [
   "Question 1","Question 2","Question 3"
] 

questionDivId =  document.getElementById('question');

setInterval(function () {
    counter--;

    if (counter >= 0) {
        id = document.getElementById('count');
        id.innerHTML = counter;
    }
    if (counter === 0) {
        id.innerHTML = 'Times Up!';
        counter = 10;
        questionsCount++;
    } 
    
    //To check if all questions are completed or not
    if (questionsCount === questions.length){
        questionDivId.innerHTML = "Well Played! Game is over";
        id.innerHTML = "";
    } else{
        questionDivId.innerHTML = questions[questionsCount];
    }   
}, 1000);

//To go to the next question
function goToNextQuestion() {
    questionsCount++;
    counter = 10;
}
<div>
    <h1 id="question"></h1>
    <h1 id="count"></h1>
    <button onclick="goToNextQuestion()">Next</button>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 2021-12-17
    • 1970-01-01
    • 2023-03-02
    • 2021-10-19
    • 2019-07-22
    • 1970-01-01
    相关资源
    最近更新 更多