From 09dbf09b5fa6d106a57cdb601c6ed6c23e00f7dd Mon Sep 17 00:00:00 2001 From: David Gay Date: Thu, 27 May 2021 19:24:12 -0400 Subject: Make timer more robust, and `stop_activity` if you can't do it --- app/javascript/controllers/timer_controller.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'app/javascript') diff --git a/app/javascript/controllers/timer_controller.js b/app/javascript/controllers/timer_controller.js index 7714bad..5190a79 100644 --- a/app/javascript/controllers/timer_controller.js +++ b/app/javascript/controllers/timer_controller.js @@ -12,6 +12,7 @@ export default class extends Controller { initialize() { this.counter = this.startValue; this.timerTarget.textContent = this.counter; + this.postFailures = 0; } connect() { @@ -23,14 +24,27 @@ export default class extends Controller { } startUpdating() { + let controller = this; this.timerInterval = setInterval(() => { - if (this.counter > 0) { - this.timerTarget.textContent = this.counter.toString(); - this.counter--; - } else if (this.counter === 0) { + 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.timerTarget.textContent = controller.counter.toString(); + controller.counter--; + } else if (this.counter <= 0) { Rails.ajax({ type: "POST", - url: this.postUrlValue, + url: controller.postUrlValue, + success: () => { + controller.postFailures = 0; + }, + error: () => { + controller.postFailures++; + }, }); } }, 1000); -- cgit v1.2.3