Add total timer

This commit is contained in:
Oskar Winkels 2020-01-15 18:02:00 +01:00 committed by Oskar
parent ee41e01519
commit 58118abb77
3 changed files with 76 additions and 31 deletions

View File

@ -46,6 +46,7 @@
<div id="quiz" class="view"> <div id="quiz" class="view">
<h1></h1> <h1></h1>
<h3 class="totaltimer">0:00</h3>
<div id="sharing"> <div id="sharing">
<input type="button" value="Share This Quiz" onclick="shareQuiz()" class="center"> <input type="button" value="Share This Quiz" onclick="shareQuiz()" class="center">
<br><a id="shareLink"></a><br> <br><a id="shareLink"></a><br>
@ -58,7 +59,8 @@
<div id="postscreen" class="view"> <div id="postscreen" class="view">
<h1></h1> <h1></h1>
<input type="button" value="Restart" onclick="startQuiz()" class="center"> <h3 class="totaltimer">0:00</h3>
<input type="button" value="Restart" onclick="reStartQuiz()" class="center">
<input type="button" value="Create New Quiz" onclick="changeView('spreadsheet')" class="center"> <input type="button" value="Create New Quiz" onclick="changeView('spreadsheet')" class="center">
</div> </div>

View File

@ -3,9 +3,11 @@ const defaultState = {
id: null, id: null,
title: null, title: null,
questions: [], questions: [],
timer: 0, success: 0,
interval: null, submitTimer: 0,
success: 0 submitInterval: null,
totalTimer: 0,
totalInterval: null,
} }
var state = defaultState var state = defaultState
@ -91,38 +93,66 @@ async function shareQuiz() {
} }
function updateTimer() { function updateTotalTimer() {
var button = document.getElementById('quizSubmitButton') var timerEls = document.querySelectorAll('.totaltimer')
if (state.timer > 0) { var minutes = Math.floor(state.totalTimer / 60)
var seconds = state.totalTimer % 60
button.value = 'Wait ' + state.timer + 's' var text = minutes + ':' + ('0' + seconds).slice(-2)
button.disabled = true
state.timer -= 1
} else { for (timerEl of timerEls)
timerEl.innerHTML = text
button.value = 'Submit Answers' state.totalTimer++
button.disabled = false
clearInterval(state.interval)
}
localStorage.setItem('state', JSON.stringify(state)) localStorage.setItem('state', JSON.stringify(state))
} }
function startTimer(time) { function startTotalTimer() {
state.timer = time || 30 console.log('Setting totalTimer to ' + state.totalTimer + ' seconds.')
console.log('Setting timer to ' + state.timer + ' seconds.') updateTotalTimer()
updateTimer() state.totalInterval = setInterval(updateTotalTimer, 1000)
state.interval = setInterval(updateTimer, 1000) }
function updateSubmitTimer() {
var button = document.getElementById('quizSubmitButton')
if (state.submitTimer > 0) {
button.value = 'Wait ' + state.submitTimer + 's'
button.disabled = true
state.submitTimer -= 1
} else {
button.value = 'Submit Answers'
button.disabled = false
clearInterval(state.submitInterval)
}
}
function startSubmitTimer(time) {
state.submitTimer = time || 30
console.log('Setting submitTimer to ' + state.submitTimer + ' seconds.')
updateSubmitTimer()
state.submitInterval = setInterval(updateSubmitTimer, 1000)
} }
@ -230,15 +260,30 @@ function startQuiz() {
changeView('quiz') changeView('quiz')
if (state.timer > 0) if (state.submitTimer > 0)
startTimer(state.timer) startSubmitTimer(state.submitTimer)
else else
updateTimer() updateSubmitTimer()
startTotalTimer()
console.log('Quiz started/resumed') console.log('Quiz started/resumed')
} }
function reStartQuiz() {
console.log('Restarting quiz');
state.totalTimer = 0
state.submitTimer = 0
state.success = 0
startQuiz()
}
function updateTitles() { function updateTitles() {
document.querySelector('#prescreen h1').innerHTML = state.title || '' document.querySelector('#prescreen h1').innerHTML = state.title || ''
document.querySelector('#quiz h1').innerHTML = state.title || '' document.querySelector('#quiz h1').innerHTML = state.title || ''
@ -275,8 +320,10 @@ function endQuiz() {
document.querySelector('#quiz form').innerHTML = '' document.querySelector('#quiz form').innerHTML = ''
changeView('postscreen') changeView('postscreen')
if (state.interval) if (state.submitInterval)
clearInterval(state.interval) clearInterval(state.submitInterval)
clearInterval(state.totalInterval)
console.log('Quiz ended') console.log('Quiz ended')
@ -335,7 +382,7 @@ function submitQuiz() {
if (!state.success) { if (!state.success) {
renderQuiz() renderQuiz()
startTimer() startSubmitTimer()
alert(text) alert(text)
} }

View File

@ -104,10 +104,6 @@ input[type="radio"]:hover + p {
background: #ddd; background: #ddd;
} }
footer {
height: 200px;
}
#sharing a { #sharing a {
font-size: 16px; font-size: 16px;
} }