Having built this, may I suggest the following.
@nick suggested using jquery countdown - good idea
function startquiz () {
var austDay = new Date();
var qtime = $('#quiz_time1').val(); // value in stored hidden input field
austDay = +qtime+'s';
$('#quizCountdown').countdown({until: austDay,
layout: 'Quiz Time Remaining: {mn} {ml}, {sn} {sl} ',
onExpiry: liftOff2
});
function liftoff2() {
$('#quizpage, #pages').toggle(); /get rid of the quiz bring back instructs
$('#quizCountdown').countdown('destroy');
}
}
function loadquiz(ptype) {
var sid = 'sid='+ $('#sid').val(); //these values are in hidden input fields
var grade = '&grade='+ $('#grade').val();
var adata = sid + grade + ptype;
$("#pageside").slideUp("fast");
$.ajax({
method: "get",url: "quiz.php",data: adata,
beforeSend: function(){
$("#loadingpage").show();}, //show loading just when link is clicked
complete: function(){
$("#loadingpage").hide();}, //stop showing loading when the process complete
success: function(html){ //so, if data is retrieved, store it in html
$("#loadingpage").hide();
$("#quizpage").show("fast"); //animation
$("#quizpage").html(html); //show the html inside #quizpage div
setupform(); // set the quiz submission ajax
startquiz(); // starts the quiz
$('#pages').toggle(); // get the directions out of the way
} }); }
This will load the timer on the quizpage from data you set via hidden fields. When the timer completes the quiz page swaps out to the page that was visible before you started the quiz.
Now, most of the magic will be in the way you set up the form (setupform(); ) to manage the submissions. We offer timed quizes in two modes. All questions at once and one question at a time. Based on the method for the quiz the set up manages how answers are submitted. A few things you need to implement...
1) When the quiz is started we store the quizid, studentid and the start time
2) When we build reports (scoring) we throw out any data that is longer than the start time for a quizid and the countdown offset, so even if someone hacks the timer, the server timestamps are the final arbitrator. The timer and auto-close are there for user convenience only. The server is the actual timekeeper.