diff options
author | David Gay <david@davidgay.org> | 2021-06-04 09:59:32 -0400 |
---|---|---|
committer | David Gay <david@davidgay.org> | 2021-06-04 09:59:32 -0400 |
commit | e3ec74cfbdf2428acb849a16b9ee6311020630a8 (patch) | |
tree | e806380d86e53e76a3589a70212fa41ddea8438a /app | |
parent | ffe82551ed64fa0ffc87ab3c0420a850a06295b4 (diff) |
Finishing timer improvements
Diffstat (limited to 'app')
-rw-r--r-- | app/javascript/controllers/timer_controller.js | 19 | ||||
-rw-r--r-- | app/views/application/_timer.html.erb | 2 |
2 files changed, 11 insertions, 10 deletions
diff --git a/app/javascript/controllers/timer_controller.js b/app/javascript/controllers/timer_controller.js index ef741bf..e85e7ca 100644 --- a/app/javascript/controllers/timer_controller.js +++ b/app/javascript/controllers/timer_controller.js @@ -5,35 +5,36 @@ export default class extends Controller { static targets = [ "timer" ]; static values = { - start: Number, + counter: Number, postUrl: String, } - initialize() { - this.counter = this.startValue; - this.timerTarget.textContent = this.counter; + connect() { + if (!this.timerInterval) this.startUpdating(); + this.timerTarget.textContent = this.counterValue; this.postFailures = 0; - this.startUpdating(); } startUpdating() { let controller = this; - this.timerInterval = setInterval(() => { + controller.timerInterval = setInterval(() => { if (controller.postFailures >= 5) { alert("An error occurred. Please submit a bug report, explaining what you were doing when it occurred."); clearInterval(controller.timerInterval); return; } - if (controller.counter > 0) controller.counter--; + if (controller.counterValue > 0) controller.counterValue--; - if (controller.counter > 0) { - controller.timerTarget.textContent = controller.counter.toString(); + if (controller.counterValue > 0) { + controller.timerTarget.textContent = controller.counterValue.toString(); } else { Rails.ajax({ type: "POST", url: controller.postUrlValue, success: () => { + clearInterval(controller.timerInterval); + controller.timerInterval = null; controller.postFailures = 0; }, error: () => { diff --git a/app/views/application/_timer.html.erb b/app/views/application/_timer.html.erb index edbc7ea..46b1134 100644 --- a/app/views/application/_timer.html.erb +++ b/app/views/application/_timer.html.erb @@ -1,7 +1,7 @@ <% if current_char.activity %> <h2 class="text-lg text-display text-center"><%= current_char.activity.name %></h2> <div data-controller="timer" - data-timer-start-value="<%= current_char.activity_time_remaining.ceil %>" + data-timer-counter-value="<%= current_char.activity_time_remaining.ceil %>" data-timer-post-url-value="<%= finish_activity_url %>" class="text-center"> <span data-timer-target="timer" class="text-3xl"></span> |