Skip to content

Commit

Permalink
Optimised auto refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
wHo69 committed Jun 24, 2022
1 parent 4f93c6e commit 14d1c0a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
20 changes: 11 additions & 9 deletions server/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ <h2 class="timing">CTSS Stall Waiting Times</h2>
<h3>{{ stall_name }}</h3>
<h4 class="waiting-time" id="{{ stall_name }}">{{ stall_info[0] }}</h4>
<p>mins</p>

<script>
(function worker() {
$.get('/api/get_timing?stall_name={{ stall_name }}', function(data) {
$('#{{ stall_name }}').html(data); // update page with new data
setTimeout(worker, 30000); // run `worker()` again after 30000ms (30s)
});
})();
</script>
</div>
{% if loop.index is divisibleby(3) %}
</div>
Expand All @@ -42,6 +33,17 @@ <h4 class="waiting-time" id="{{ stall_name }}">{{ stall_info[0] }}</h4>
{% endfor %}
</div>
</div>

<script>
(function worker() {
$.getJSON('/api/get_timing?stall_name=all', function(data) {
$.each(data, function(stall_name, stall_info) {
$("#" + stall_name).text(stall_info[0]);
})
setTimeout(worker, 30000); // run worker() again after 30000ms (30s)
});
})();
</script>
</body>

<style>
Expand Down
8 changes: 5 additions & 3 deletions server/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
# Displayed via GET request to /
# / should auto refresh once every minute

# TODO: Refresh once every minute

# Libraries
import flask
from flask import render_template, request
from flask import jsonify, render_template, request

import os
import time
Expand Down Expand Up @@ -84,6 +82,10 @@ def get_timing():
if "stall_name" not in request.args:
return "Missing stall name", 400

# Check if stall name is all
if request.args["stall_name"] == "all":
return jsonify(timings), 200

# Check if stall name is valid
if request.args["stall_name"] not in stall_names:
return "Invalid stall name", 400
Expand Down

0 comments on commit 14d1c0a

Please sign in to comment.