summaryrefslogtreecommitdiff
path: root/app/javascript/controllers
diff options
context:
space:
mode:
authorDavid Gay <david@davidgay.org>2021-05-04 17:55:28 -0400
committerDavid Gay <david@davidgay.org>2021-05-04 17:57:12 -0400
commit73744a9c6840fb0ba6f285ca81f9fba75ec22d5f (patch)
tree837333e9e46c5ccc6cf50214a94c2b9b6d0bb7f3 /app/javascript/controllers
parentdddbf75428477f5e073584939d098e55d6324be3 (diff)
Initial draft of timer setup, with results outputting and items being awarded
Diffstat (limited to 'app/javascript/controllers')
-rw-r--r--app/javascript/controllers/activities/timer_controller.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/app/javascript/controllers/activities/timer_controller.js b/app/javascript/controllers/activities/timer_controller.js
new file mode 100644
index 0000000..11057c0
--- /dev/null
+++ b/app/javascript/controllers/activities/timer_controller.js
@@ -0,0 +1,43 @@
+import { Controller } from "stimulus";
+import Rails from "@rails/ujs";
+
+export default class extends Controller {
+ static targets = [ "timer" ];
+
+ static values = {
+ start: Number,
+ }
+
+ initialize() {
+ this.counter = this.startValue;
+ this.timerTarget.textContent = this.counter;
+ }
+
+ connect() {
+ this.startUpdating();
+ }
+
+ disconnect() {
+ this.stopUpdating();
+ }
+
+ startUpdating() {
+ this.timerInterval = setInterval(() => {
+ if (this.counter > 0) {
+ this.timerTarget.textContent = this.counter.toString();
+ this.counter--;
+ } else if (this.counter === 0) {
+ Rails.ajax({
+ type: "POST",
+ url: "/finish_activity",
+ });
+ }
+ }, 1000);
+ }
+
+ stopUpdating() {
+ if (this.timerInterval) {
+ clearInterval(this.timerInterval);
+ }
+ }
+}