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/javascript | |
parent | ffe82551ed64fa0ffc87ab3c0420a850a06295b4 (diff) |
Finishing timer improvements
Diffstat (limited to 'app/javascript')
-rw-r--r-- | app/javascript/controllers/timer_controller.js | 19 |
1 files changed, 10 insertions, 9 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: () => { |