From 23f961b19c81bed988e82b4f7fc0da0b45ebbb39 Mon Sep 17 00:00:00 2001 From: Unboring SL Date: Mon, 20 Jan 2020 12:55:55 +0100 Subject: [PATCH] Add countdown watch --- src/components/watch.js | 32 ++++++++++++++++++++++++++++++++ src/home.html | 29 +++++++++++++++++++++++++++-- src/index.js | 1 + 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/components/watch.js diff --git a/src/components/watch.js b/src/components/watch.js new file mode 100644 index 0000000..c923f06 --- /dev/null +++ b/src/components/watch.js @@ -0,0 +1,32 @@ +AFRAME.registerComponent("watch", { + init: function() { + this.timer = AFRAME.utils.getUrlParameter('timer') + if (!this.timer) this.timer = 300 //5min + document.querySelector("#watch").setAttribute("text","value:" + this.formatSeconds(this.timer)) + this.tick = AFRAME.utils.throttleTick(this.tick, 1000, this); + // details https://aframe.io/docs/1.0.0/core/utils.html#aframe-utils-throttle-function-minimuminterval-optionalcontext + }, + + formatSeconds: function(secs) { + function pad(n) { + return (n < 10 ? "0" + n : n) + } + + var m = Math.floor(secs / 60); + var s = Math.floor(secs - m * 60); + + return pad(m) + ":" + pad(s); + }, + + tick: function (t, dt) { + var time = Number( this.timer ) + console.log(this.timer); + if(this.timer > 0){ + this.timer-- + } else { + this.timer = 0 + document.querySelector("#endMessage").setAttribute("visible","true") + } + document.querySelector("#watch").setAttribute("text","value:"+ this.formatSeconds(time)) + }, +}); diff --git a/src/home.html b/src/home.html index 9f8cb0a..eade8ca 100644 --- a/src/home.html +++ b/src/home.html @@ -13,7 +13,29 @@ material="color: #EF2D5E;"> - + + + + + + + @@ -21,6 +43,9 @@ - + + + \ No newline at end of file diff --git a/src/index.js b/src/index.js index 987a9fc..a94b9ce 100644 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,7 @@ require('aframe-event-set-component'); require('./components/aabb-collider'); require('./components/grab'); +require('./components/watch'); require('./index.css')