1 回答

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊
這是適合您的基本工作代碼。玩一玩它以獲得您想要的結(jié)果。祝您編碼愉快!
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>
添加回答
舉報(bào)