Bläddra i källkod

Fix small timer issues

JoostSijm 6 år sedan
förälder
incheckning
d6fb0a5af9

+ 2 - 0
app/modules/vote/templates/vote/public/view.j2

@@ -44,10 +44,12 @@
                 <th scope="row">Eindigt</th>
                 <td>{{ ballot.end_at }}</td>
             </tr>
+            {% if ballot.active() %}
             <tr>
                 <th scope="row">Te gaan</th>
                 <td class="countdown" date="{{ ballot.end_at }}"><span class="hours">00</span>:<span class="minutes">00</span>:<span class="seconds">00</span></td>
             </tr>
+            {% endif %}
             <tr>
                 <th scope="row">Aangemaakt door</th>
                 <td>{{ ballot.user.name }}</td>

+ 2 - 0
app/modules/vote/templates/vote/view.j2

@@ -21,10 +21,12 @@
 		<th scope="row">End</th>
 		<td>{{ ballot.end_at }}</td>
 	</tr>
+	{% if ballot.active() %}
     <tr>
         <th scope="row">Te gaan</th>
         <td class="countdown" date="{{ ballot.end_at }}"><span class="hours">00</span>:<span class="minutes">00</span>:<span class="seconds">00</span></td>
     </tr>
+    {% endif %}
 	<tr>
 		<th scope="row">User</th>
 		<td>{{ ballot.user.name }}</td>

+ 12 - 9
app/static/js/index.js

@@ -9,7 +9,7 @@ import '../css/index.css'
 import SimpleMDE from 'simplemde'
 import 'simplemde/dist/simplemde.min.css'
 
-if (document.querySelector('textarea') > 0) {
+if (document.querySelector('textarea') != null) {
   new SimpleMDE()
 }
 
@@ -24,13 +24,14 @@ function countdown() {
     return;
   }
 
-  calculate()
-  setInterval(calculate, 1000);
-  
+  let startDate = new Date().getTime();
+  if (endDate > startDate) {
+    calculate()
+    setInterval(calculate, 1000);
+  }
+
   function calculate() {
-    let startDate = new Date();
-    startDate = startDate.getTime();
-    
+    let startDate = new Date().getTime();
     let timeRemaining = parseInt((endDate - startDate) / 1000);
     
     if (timeRemaining >= 0) {
@@ -46,11 +47,13 @@ function countdown() {
       document.querySelector(".countdown .minutes").innerHTML = ("0" + minutes).slice(-2);
       document.querySelector(".countdown .seconds").innerHTML = ("0" + seconds).slice(-2);
     } else {
-      return;
+      window.location.reload(false); 
     }
   }
 }
 
 window.onload = function () {
-  countdown();
+  if (document.querySelector('.countdown') != null) {
+    countdown();
+  }
 }