make timer configurable
authorLaurent Mazet <mazet@softndesign.org>
Tue, 18 Aug 2020 11:44:25 +0000 (13:44 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Tue, 18 Aug 2020 11:44:25 +0000 (13:44 +0200)
public/index.html
public/trivabble.js

index 6b8cbfe2e74b4c341a31ed19ca50aca40fa6d450..0f64a51f66325cebd10c834bba8eca00d8662e05 100644 (file)
@@ -53,6 +53,7 @@
                 <div id="prefs-sound">
                     <p><label><input type="checkbox" id="tiles-sound" checked="checked" /><span data-l10n="text-content">Sound of the tiles</span></label></p>
                     <p><label><input type="checkbox" id="msg-sound" checked="checked" /><span data-l10n="text-content">Sound of messages</span></label></p>
+                    <p><label><input type="checkbox" id="enable-timer" /> <span data-l10n="text-content">Enable timer</span></label></p>
                 </div>
             </div>
         </div>
index 460a5ce9246096309142f4d3f8c36047f08ad222..78361c2846b0163478e86e5b9d9ce7cd3b876d6c 100644 (file)
         if (!Object.prototype.hasOwnProperty.call(localStorage, "timerEnable")) {
             localStorage.timerEnable = Conf.ENABLE_TIMER;
         }
-        if (localStorage.timerEnable) {
-            activateTimer();
-        } else {
-            desactivateTimer();
-        }
-    }
-
-    function activateTimer() {
-        document.getElementById("timer").style.display = "block";
-        localStorage.timer = setInterval(function() {
-            document.getElementById("timer-turn").innerHTML = timerString(++localStorage.timerTurn)
-            document.getElementById("timer-global").innerHTML = timerString(++localStorage.timerGlobal);
-        }, 1000);
+        document.getElementById("enable-timer").onclick = function() {
+            setTimerState(document.getElementById("enable-timer").checked);
+        };
+        setTimerState (localStorage.timerEnable);
     }
 
-    function desactivateTimer() {
-        document.getElementById("timer").style.display = "none";
-        if (Object.prototype.hasOwnProperty.call(localStorage, "timer")) {
-            clearInterval(localStorage.timer);
-            delete(localStorage.timer);
+    function setTimerState(state) {
+        if (state) {
+            document.getElementById("timer").style.display = "block";
+            document.getElementById("enable-timer").checked = true;
+            localStorage.timer = setInterval(function() {
+                document.getElementById("timer-turn").innerHTML = timerString(++localStorage.timerTurn)
+                document.getElementById("timer-global").innerHTML = timerString(++localStorage.timerGlobal);
+            }, 1000);
+        } else {
+            document.getElementById("timer").style.display = "none";
+            document.getElementById("enable-timer").checked = false;
+            if (Object.prototype.hasOwnProperty.call(localStorage, "timer")) {
+                clearInterval(localStorage.timer);
+                delete(localStorage.timer);
+            }
+            localStorage.timerGlobal = localStorage.timerTurn = 0;
         }
     }
 
-
     function repromptName(f) {
         if (localStorage.trivabblePlayerName && localStorage.trivabblePlayerName.trim()) {
             f();