I am trying to set a timer for users taking test.
The timer get a value from PHP and the jquery function is meant to initiate a timer countdown until the set time elapses.
On submission of the test form:
the function checks whether the time has elapsed, if YES, it submits all questions and whatever answered available. else if NO, it checks to see if user attempted all questions and then trigger a prompt box to notify user about any question yet to be attempted before finally submitting the test form.
So far, the time set for the test is being displayed but countdown is not working i.e the time is not counting down. Not sure but could be from the setTimeout().
Here is a jsfiddle link
Below is the jquery:
$(document).ready(function(){
var mins = <?php echo $rw_test['duration']; ?>;
var secs = mins * 60;
var currentSeconds = 0;
var currentMinutes = 0;
Decrement = function(){
currentMinutes = Math.floor(secs / 60);
currentSeconds = secs % 60;
if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds;
secs--;
var htmlVar = currentMinutes + ":" + currentSeconds;
$('#timerText').html(htmlVar);
if(secs !== -1) setTimeout(function(){},1000);
if(currentSeconds == 00 && currentMinutes==0){
/************** If User time has elapse: Submit form ****************/
document.getElementById('myquiz').submit();
//document.myquiz.submit();
/************** If User time has elapse: Submit form ****************/
}
else{
/************** User time still yet to elapse and form was submitted by user: check if all questions were answered ****************/
$('#myquiz').submit(function(){
var errors = [];
$('.content_question').each(function(){
var answerCont = $(this).next();
var inputs = $(answerCont).find('input[type=radio], input[type=checkbox]');
if(inputs.length > 0)
{
var groupChecked = false;
$(inputs).each(function(){
if($(this).is(':checked'))
{
groupChecked = true;
}
});
if(!groupChecked)
{
errors.push($(this).text().trim());
}
}
});
if(errors.length > 0)
{
var errorMessage = "You forgot to attempt " + errors.length + " questions: \n\n";
for(var i=0; i<errors.length; i++)
{
errorMessage += errors[i] + "\n";
}
//alert(errorMessage);
//return false;
return confirm(errorMessage+' \n\n Do you want to continue by ending the test?');
}
});
/************** User time still yet to elapse and form was submitted by user: check if all questions were answered ****************/
}
};
setTimeout(Decrement(),1000); // Initiate timer and begin to Decrement user time till time elapse
});
Here is the html:
<div><span id="timerText"></span> (Remaining test time)</div>
Would be pleased getting some help with this.
Thanks.