Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions tubes/templates/tubes/proposal_fight.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,24 @@ <h2>Submit answer</h2>
<script>
(function() {
const el = document.getElementById("countdown");
let secs = parseInt(el.dataset.seconds, 10);
const deadline = Date.now() + parseInt(el.dataset.seconds, 10) * 1000;
let interval = null;
let finished = false;

function fmt(s) {
return Math.floor(s / 60) + ":" + String(s % 60).padStart(2, "0");
}
el.textContent = fmt(secs);
const interval = setInterval(function() {
secs -= 1;

function tick() {
if (finished) {
return;
}
const secs = Math.max(0, Math.round((deadline - Date.now()) / 1000));
if (secs <= 0) {
clearInterval(interval);
finished = true;
if (interval !== null) {
clearInterval(interval);
}
el.textContent = "Time's up!";
el.classList.add("text-danger");
setTimeout(function() {
Expand All @@ -69,7 +77,17 @@ <h2>Submit answer</h2>
} else {
el.textContent = fmt(secs);
}
}, 1000);
}

tick();
if (!finished) {
interval = setInterval(tick, 1000);
document.addEventListener("visibilitychange", function() {
if (!document.hidden) {
tick();
}
});
}
})();
</script>
{% endblock layout-content %}
Loading