Skip to content
Permalink
Browse files

simplify timer

  • Loading branch information...
akshayravikumar committed Aug 8, 2019
1 parent 5b35b3d commit c0e0a132a313abe56b0be0d640906d0f5b5cc04d
Showing with 8 additions and 14 deletions.
  1. +7 −13 assets/js/index.js
  2. +1 −1 index.html
@@ -12,24 +12,18 @@ function loadBodyText() {
}

// A simple timer
toHHMMSS = function(secs) {
var sec_num = parseInt(secs, 10)
var hours = Math.floor(sec_num / 3600)
var minutes = Math.floor(sec_num / 60) % 60
var seconds = sec_num % 60

return [hours,minutes,seconds]
.map(v => v < 10 ? "0" + v : v)
.filter((v,i) => v !== "00" || i > 0)
.join(":")
function displayTime(secs) {
let minutes = Math.floor(secs / 60) % 60;
let seconds = secs % 60;
let displayText = minutes + ":" + (seconds < 10 ? "0" + seconds : seconds);
$("#timer").text(displayText);
}

function startTimer(onTimeoutFunc) {
secondsRemaining = TIMEOUT_SECONDS;
$("#timer").text(toHHMMSS(secondsRemaining));
setInterval(function() {
secondsRemaining--;
$("#timer").text(toHHMMSS(secondsRemaining));
displayTime(secondsRemaining);
if (secondsRemaining == 0) {
clearInterval();
onTimeoutFunc();
@@ -63,7 +57,6 @@ function endGame(numCorrect) {
$("#intro-window").hide();
$("#game-window").hide();
$("#ending-window").show();
$("#timer").text(toHHMMSS(TIMEOUT_SECONDS));
loadBodyText();

let problemsText = numCorrect + ((numCorrect == 1) ? " problem" : " problems");
@@ -91,6 +84,7 @@ function startGame() {
let problemNumber = 0;
let numCorrect = 0;
$("#score").text(0);
displayTime(TIMEOUT_SECONDS);

// Reset and start the timer
loadProblem();
@@ -64,7 +64,7 @@
<div id="top-row">
<button id="skip-button" class="latex-button">\(\text{Skip This Problem}\)</button>
<span id="score-wrapper">\(\text{Score:\,}\) <span id="score">0</span></span>
<span id="timer-wrapper">\(\text{Time:\,}\) <span id="timer">00:00</span></span>
<span id="timer-wrapper">\(\text{Time:\,}\) <span id="timer"></span></span>
</div>

<br><br>

0 comments on commit c0e0a13

Please sign in to comment.
You can’t perform that action at this time.