I am developing a test engine web application. Users will be give some amount of time to answer questions. I want to create a countdown clock after which the test is finished. I’m using javascript right now but facing problems of refresh and back button in the browser.
Following is the javascript:
<script>
function countDown (count) {
if (count > 0) {
var hours = Math.floor(count/3600)
var minutes = Math.floor(count/60) - (hours*60)
var seconds = count - (minutes * 60) - (hours*3600)
var d = document.getElementById("countDiv");
d.innerHTML = hours + ":" + minutes + ":" + seconds;
else
document.location = "test_finished.html";
}
countDown(<%=@totaltime%>);
</script>
How can i disable the refresh and back button or some other workaround??
You cannot enforce such things using JavaScript.
One way would be to have records in the database which hold the test start date time in it, once a user starts a test the start time is set, upon loading the initial timer value is calculated using it, so you can have back and refresh working fine and the countdown timer always being accurate.