Open the project with visual studio
Go to /Views/StudentExam/TakeExam.cshtml
Scroll down until you see the “function startTimer”, change it to the code below.
function startTimer(duration) {
var timerid = "totalSecondsLeft" + "@Model.StudentId" + "@Model.ExamId";
let durationInSecond = duration * 60;
if (localStorage.getItem(timerid) == null) {
localStorage.setItem(timerid, durationInSecond);
}
// get time remaining from local storage or set to duration if not found
let totalSecondsLeft = localStorage.getItem(timerid) || durationInSecond;
interval = setInterval(() => {
let totalSecondsLeft = localStorage.getItem(timerid);
totalSecondsLeft--;
localStorage.setItem(timerid, totalSecondsLeft);
const hours = Math.floor(totalSecondsLeft / 3600);
totalSecondsLeft = totalSecondsLeft % 3600;
const minutes = Math.floor(totalSecondsLeft / 60);
const seconds = totalSecondsLeft % 60;
let timer = document.getElementById("timer");
timer.innerHTML = hours + ':' + minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0');
if (totalSecondsLeft === 0) {
clearInterval(interval);
localStorage.removeItem(timerid);
timer.innerHTML = "Time is up!";
window.location.href = "@Url.Action("timesup","studentexam")" + "?eId=" + "@Model.ExamId" + "&sId=" + "@Model.StudentId";
return;
}
}, 1000);
}
Done! Now try to test with a new exam.