This is my snippet.
$(function() {
setCountDown(0, 1, 0);
a = Array();
$(window).bind('beforeunload', function() {
if (a['endTime']) {
return ("You are in the middle of a test");
}
});
});
function setCountDown(hours, minutes, seconds) {
$(".timer").removeClass("common_displayNone");
timerInterval = setInterval(
function() {
if (hours.toString().length == 1) {
hours = "0" + hours;
}
if (minutes.toString().length == 1) {
minutes = "0" + minutes;
}
if (seconds.toString().length == 1) {
seconds = "0" + seconds;
}
if (seconds > 0) {
seconds--;
if (seconds.toString().length == 1) {
seconds = "0" + seconds;
}
} else if (seconds == 0 && (minutes > 0)) {
minutes = minutes - 1;
seconds = 59;
} else if (minutes == 0 && (hours > 0)) {
hours = hours - 1;
minutes = 59;
}
if (hours == 0 && minutes == 0 && seconds == 0) {
clearInterval(timerInterval);
a.removeItem("endTime");
}
$("#timer").html(hours + ":" + minutes + ":" + seconds);
a['endTime'] = (hours * 3600 + minutes * 60 + seconds);
}, 1000);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="timer"></div>
Just try to reload the page. You will get an alert. During that alert, my timers are not running. So user can cheat test. How to solve this issue?