fix(lms): js timer form auto-submit and late POST saving
This commit is contained in:
@@ -125,52 +125,54 @@
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
let timeLeft = {{ time_left_seconds }};
|
||||
const display = document.getElementById('countdown-display');
|
||||
const form = document.getElementById('assignment-form');
|
||||
|
||||
function updateTimer() {
|
||||
if (timeLeft <= 0) {
|
||||
display.textContent = '00:00:00';
|
||||
display.classList.add('text-red-300');
|
||||
// Auto submit
|
||||
if (form) {
|
||||
// Prevent multiple submissions
|
||||
form.onsubmit = null;
|
||||
const btn = form.querySelector('button[type="submit"]');
|
||||
if (btn) btn.disabled = true;
|
||||
|
||||
// Append a hidden flag
|
||||
const input = document.createElement('input');
|
||||
input.type = 'hidden';
|
||||
input.name = 'auto_submit';
|
||||
input.value = '1';
|
||||
form.appendChild(input);
|
||||
|
||||
form.submit();
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
let timeLeft = {{ time_left_seconds }};
|
||||
const display = document.getElementById('countdown-display');
|
||||
const form = document.getElementById('assignment-form');
|
||||
|
||||
function updateTimer() {
|
||||
if (timeLeft <= 0) {
|
||||
display.textContent = '00:00:00';
|
||||
display.classList.add('text-red-300');
|
||||
// Auto submit
|
||||
if (form) {
|
||||
// Prevent multiple submissions
|
||||
form.onsubmit = null;
|
||||
const btn = form.querySelector('button[type="submit"]');
|
||||
if (btn) btn.disabled = true;
|
||||
|
||||
// Append a hidden flag
|
||||
const input = document.createElement('input');
|
||||
input.type = 'hidden';
|
||||
input.name = 'auto_submit';
|
||||
input.value = '1';
|
||||
form.appendChild(input);
|
||||
|
||||
form.submit();
|
||||
}
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const h = Math.floor(timeLeft / 3600);
|
||||
const m = Math.floor((timeLeft % 3600) / 60);
|
||||
const s = timeLeft % 60;
|
||||
display.textContent =
|
||||
(h > 0 ? (h < 10 ? '0' + h : h) + ':' : '') +
|
||||
(m < 10 ? '0' + m : m) + ':' +
|
||||
(s < 10 ? '0' + s : s);
|
||||
|
||||
if (timeLeft < 60) {
|
||||
display.classList.add('text-red-300', 'animate-pulse');
|
||||
const h = Math.floor(timeLeft / 3600);
|
||||
const m = Math.floor((timeLeft % 3600) / 60);
|
||||
const s = timeLeft % 60;
|
||||
display.textContent =
|
||||
(h > 0 ? (h < 10 ? '0' + h : h) + ':' : '') +
|
||||
(m < 10 ? '0' + m : m) + ':' +
|
||||
(s < 10 ? '0' + s : s);
|
||||
|
||||
if (timeLeft < 60) {
|
||||
display.classList.add('text-red-300', 'animate-pulse');
|
||||
}
|
||||
|
||||
timeLeft--;
|
||||
setTimeout(updateTimer, 1000);
|
||||
}
|
||||
|
||||
timeLeft--;
|
||||
setTimeout(updateTimer, 1000);
|
||||
}
|
||||
|
||||
if (timeLeft > 0) {
|
||||
updateTimer();
|
||||
}
|
||||
if (timeLeft > 0) {
|
||||
updateTimer();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
{% endif %}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user