Fix countdown back bug

This commit is contained in:
cuom1999 2021-12-10 16:36:55 -06:00
parent 16974ab3dc
commit fea5e46cda
7 changed files with 20 additions and 19 deletions

View file

@ -24,5 +24,7 @@ def seconds(timedelta):
@registry.filter @registry.filter
@registry.render_with('time-remaining-fragment.html') @registry.render_with('time-remaining-fragment.html')
def as_countdown(timedelta): def as_countdown(time):
return {'countdown': timedelta} time_now = datetime.datetime.now(datetime.timezone.utc)
initial = abs(time - time_now)
return {'countdown': time, 'initial': initial}

View file

@ -183,8 +183,7 @@ if (!Date.now) {
} }
function count_down(label) { function count_down(label) {
var initial = parseInt(label.attr('data-secs')); var end_time = new Date(label.attr('data-secs'));
var start = Date.now();
function format(num) { function format(num) {
var s = "0" + num; var s = "0" + num;
@ -192,7 +191,7 @@ function count_down(label) {
} }
var timer = setInterval(function () { var timer = setInterval(function () {
var time = Math.round(initial - (Date.now() - start) / 1000); var time = Math.round((end_time - Date.now()) / 1000);
if (time <= 0) { if (time <= 0) {
clearInterval(timer); clearInterval(timer);
setTimeout(function() { setTimeout(function() {

View file

@ -289,7 +289,7 @@
{% if request.participation.spectate %} {% if request.participation.spectate %}
{{ _('spectating') }} {{ _('spectating') }}
{% elif request.participation.end_time %} {% elif request.participation.end_time %}
<div id="contest-time-remaining" data-secs="{{ request.participation.time_remaining|seconds }}"> <div id="contest-time-remaining" data-secs="{{request.participation.end_time}}">
{{ request.participation.time_remaining|timedelta("localized") }} {{ request.participation.time_remaining|timedelta("localized") }}
</div> </div>
{% else %} {% else %}

View file

@ -153,7 +153,7 @@
<a href="{{ url('contest_view', contest.key) }}">{{ contest.name }}</a> <a href="{{ url('contest_view', contest.key) }}">{{ contest.name }}</a>
</div> </div>
<div class="time"> <div class="time">
{{ _('Ends in %(countdown)s.', countdown=contest.time_before_end|as_countdown) }} {{ _('Ends in %(countdown)s.', countdown=contest.end_time|as_countdown) }}
</div> </div>
</div> </div>
{% endfor %} {% endfor %}
@ -171,7 +171,7 @@
<a href="{{ url('contest_view', contest.key) }}">{{ contest.name }}</a> <a href="{{ url('contest_view', contest.key) }}">{{ contest.name }}</a>
</div> </div>
<div class="time"> <div class="time">
{{ _('Starting in %(countdown)s.', countdown=contest.time_before_start|as_countdown) }} {{ _('Starting in %(countdown)s.', countdown=contest.start_time|as_countdown) }}
</div> </div>
</div> </div>
{% endfor %} {% endfor %}

View file

@ -30,26 +30,26 @@
{{- contest.start_time|utc|date('Y-m-d\TH:i:s') }}" class="date"> {{- contest.start_time|utc|date('Y-m-d\TH:i:s') }}" class="date">
{%- if contest.is_in_contest(request.user) and not request.participation.live -%} {%- if contest.is_in_contest(request.user) and not request.participation.live -%}
{% if request.participation.spectate %} {% if request.participation.spectate %}
{{- _('Spectating, contest ends in %(countdown)s.', countdown=contest.time_before_end|as_countdown) -}} {{- _('Spectating, contest ends in %(countdown)s.', countdown=contest.end_time|as_countdown) -}}
{% elif request.participation.end_time %} {% elif request.participation.end_time %}
{{- _('Participating virtually, %(countdown)s remaining.', countdown=request.participation.time_remaining|as_countdown) -}} {{- _('Participating virtually, %(countdown)s remaining.', countdown=request.participation.end_time|as_countdown) -}}
{% else %} {% else %}
{{- _('Participating virtually.') -}} {{- _('Participating virtually.') -}}
{% endif %} {% endif %}
{%- else -%} {%- else -%}
{% if contest.start_time > now %} {% if contest.start_time > now %}
{{- _('Starting in %(countdown)s', countdown=contest.time_before_start|as_countdown) -}} {{- _('Starting in %(countdown)s', countdown=contest.start_time|as_countdown) -}}
{% elif contest.end_time < now %} {% elif contest.end_time < now %}
{{- _('Contest is over.') -}} {{- _('Contest is over.') -}}
{% else %} {% else %}
{%- if has_joined -%} {%- if has_joined -%}
{% if live_participation.ended %} {% if live_participation.ended %}
{{- _('Your time is up! Contest ends in %(countdown)s.', countdown=contest.time_before_end|as_countdown) -}} {{- _('Your time is up! Contest ends in %(countdown)s.', countdown=contest.end_time|as_countdown) -}}
{% else %} {% else %}
{{- _('You have %(countdown)s remaining.', countdown=live_participation.time_remaining|as_countdown) -}} {{- _('You have %(countdown)s remaining.', countdown=live_participation.end_time|as_countdown) -}}
{% endif %} {% endif %}
{%- else -%} {%- else -%}
{{ _('Contest ends in %(countdown)s.', countdown=contest.time_before_end|as_countdown) }} {{ _('Contest ends in %(countdown)s.', countdown=contest.end_time|as_countdown) }}
{%- endif -%} {%- endif -%}
{% endif %} {% endif %}
{%- endif -%} {%- endif -%}

View file

@ -252,7 +252,7 @@
{% if contest.time_limit %} {% if contest.time_limit %}
<span class="time">{{ _('Window ends in %(countdown)s', countdown=participation.time_remaining|as_countdown)}} <span class="time">{{ _('Window ends in %(countdown)s', countdown=participation.time_remaining|as_countdown)}}
{% elif contest.time_before_end %} {% elif contest.time_before_end %}
<span class="time">{{ _('Ends in %(countdown)s', countdown=contest.time_before_end|as_countdown) }}</span> <span class="time">{{ _('Ends in %(countdown)s', countdown=contest.end_time|as_countdown) }}</span>
{% endif %} {% endif %}
{{ time_left(contest) }} {{ time_left(contest) }}
{% endif %} {% endif %}
@ -301,7 +301,7 @@
{% if contest.start_time %} {% if contest.start_time %}
<br> <br>
{% if contest.time_before_end %} {% if contest.time_before_end %}
<span class="time">{{ _('Ends in %(countdown)s', countdown=contest.time_before_end|as_countdown) }}</span> <span class="time">{{ _('Ends in %(countdown)s', countdown=contest.end_time|as_countdown) }}</span>
{% endif %} {% endif %}
{{ time_left(contest) }} {{ time_left(contest) }}
{% endif %} {% endif %}
@ -336,7 +336,7 @@
{% if contest.start_time %} {% if contest.start_time %}
<br> <br>
{% if contest.time_before_start %} {% if contest.time_before_start %}
<span class="time">{{ _('Starting in %(countdown)s.', countdown=contest.time_before_start|as_countdown) }}</span> <span class="time">{{ _('Starting in %(countdown)s.', countdown=contest.start_time|as_countdown) }}</span>
{% endif %} {% endif %}
{{ time_left(contest) }} {{ time_left(contest) }}
{% endif %} {% endif %}

View file

@ -1,3 +1,3 @@
<span data-secs="{{ countdown|seconds }}" class="time-remaining"> <span data-secs="{{ countdown }}" class="time-remaining">
{{ countdown|timedelta("localized") }} {{ initial|timedelta("localized") }}
</span> </span>