I am trying to develop a quiz app with Django. Where I want to add a functionality like this - "When the users will click on start test button, they will be redirected to the questions page, and exam time will be started". Here I am using JavaScript "setInterval" to count time. It's working on the same page, but it's not working on the redirected page.
My Js code :
var check = null;
function printDuration() {
if (check == null) {
var cnt = 0;
check = setInterval(function () {
cnt += 1;
document.getElementById("para").innerHTML = cnt;
}, 1000);
}
}
function stop() {
clearInterval(check);
check = null;
document.getElementById("para").innerHTML = '0';
}
console.log("Time counting... ... ...")
Button
<a href="{% url 'test' %}" class="btn btn-primary mt-5"id="btnStart" type="button" value="Start"
onclick="printDuration();">Start Test</a>
Redirected page code: , here time counting will be started:
<h2 class="pb-5">Welcome to test page</h2>
<p id="para">0</p>
If I understand correctly, the timer is on the page before redirect. When you go to a new url, everything reloads including the JavaScript. Everything from the previous page including your timer is removed. You'll have to make a new timer in the redirected page