Hey guys, I'm making a quiz with a limit, I want to keep track of the time left with something other than javascript, Because with javascript the user could simply pause the timer by disabling javascript, and take as much time as they need. and when they are done they could simply turn JS back on and submit the quiz. I'm using coldfusion if this helps, thanks in advance.
You should monitor both user and server side. Javascript is fine for user side, to give them an alert when they're close to running out of time, but you should also check server side when it's submitted. I'd recommend having a hidden field attached in the test that has a timestamp of the time the test was started, then when submitted you can compare the time started to time submitted to ensure they haven't exceeded their time. Never rely on client side verification only.
Edit Another alternative, as per the comments, would be to have the database where you keep their test scores timestamp the time they start the test, then when submitted the server compares the times and if they don't match they get nothing. This cuts the user completely out, including time zones, as everything compared is done by server time by the server. I'd still recommened tracking time with javascript on user side for those non-evil users that just want to know how much time they have.
var oReq = getXMLHttpRequest();
function getXMLHttpRequest() {
if (window.XMLHttpRequest) {
return new window.XMLHttpRequest;
}
else {
try {
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
catch (ex) {
return null;
}
}
}
and handle this at server side by calling method zx
function ZX(q, w, e, t) {
if (oReq != null) {
oReq.open("GET", "XML.xml", true);
oReq.onreadystatechange = function () {
//work with response xml object
alert(oReq.responseXML.getElementsByTagName("")[0].getAttribute("tag1"));
if (oReq.readyState == 4 /* complete */) {
if (oReq.status == 200) {
}
}
};
oReq.send();
}
else {
window.alert("AJAX (XMLHTTP) not supported.");
}
}
Assuming you are using a session, I would log the time the user requested the page and the time the user has submitted the quiz. I would still use Javascript to display them the time left, but just for that.
One approach, assuming you want your server to take action in the event that they didn't respond in time, would be to spawn a separate thread with cfthread, have it sleep the length of the quiz, and if their answers haven't been submitted, do whatever. I imagine something like..
.. start of quiz stuff ..
<cfthread action="run" name="quiztimer">
<cfthread action="sleep" duration="120000" />
<cfquery>fetch quiz results</cfquery>
<cfif NoQuizAnswers>
<cfinsert>insert failure into quiz results</cfinsert>
</cfif>
</cfthread>