Skip to content

Commit

Permalink
simplify timer
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Ravikumar committed Aug 8, 2019
1 parent 5b35b3d commit c0e0a13
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
20 changes: 7 additions & 13 deletions assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -91,6 +84,7 @@ function startGame() {
let problemNumber = 0;
let numCorrect = 0;
$("#score").text(0);
displayTime(TIMEOUT_SECONDS);

// Reset and start the timer
loadProblem();
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit c0e0a13

Please sign in to comment.