From e3ec74cfbdf2428acb849a16b9ee6311020630a8 Mon Sep 17 00:00:00 2001 From: David Gay Date: Fri, 4 Jun 2021 09:59:32 -0400 Subject: Finishing timer improvements --- app/javascript/controllers/timer_controller.js | 19 ++++++++++--------- app/views/application/_timer.html.erb | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) (limited to 'app') 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 %>

<%= current_char.activity.name %>

-- cgit v1.2.3