FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP, AJAX and setInterval. Can't make a stopwatch





binsmyth
We are trying to build an online testing system. Which has a lot of feature.
There is a student part and there is a teacher part.
A Student can login and do the test while teacher authorizes the test.
There is a countdown timer in the test.
We are using a session variable , AJAX and setinterval to countdown the timer. But the other thing is that we should also put a function into the teacher side that they can pause a test and resume a test.
Well at first I thought I could make it pause by using the AJAX. But actually it does not work I don't know what is the problem. Either the setinterval or the SESSION variable.

Code:

session_start();
(.....Get Time Limit From Database)
$TimeLimit = $Duration;
if(!isset($_SESSION["startTime"])){$_SESSION["startTime"] = mktime(date(G),date(i),date(s),date(m),date(d),date(Y)) + $TimeLimit + 1;}
?>

<script>
var ct = setInterval("calculate_time()",100); // Start clock.
function calculate_time()
{
 var active = ajaxFunction();
 if (active == 0) {   clearInterval(ct);   return false; }
 
 if (active == 1) {
     ct = setInterval(this,100);
     var end_time = "<?php echo $_SESSION[startTime] ; ?>";
    var dt = new Date();                           
    var time_stamp = dt.getTime()/1000;                
    var total_time = end_time - Math.round(time_stamp);    
    var mins = Math.floor(total_time / 60);             
    var secs = total_time - (mins * 60);                
    if(secs < 10){secs = "0" + secs;}                   
}
 document.getElementById("txt").value = mins + ":" + secs;
 (..........)
}
 function ajaxFunction(url)
{...............;xmlhttp.open("GET","checkpause.php",true);xmlhttp.send(null);return a;}

Could Not get the whole code in maybe this would be ok
Peterssidan
You mean the teacher will be able to pause the test during the test and during the pause the student can't continue? Am I right? It's web based so I assume the teacher will have no control over the student's computer so all the student has to do is to copy paste the test and think about it during the pause.

comments on the code. shouldn't ajaxFunction be passed an argument? what is returned? (what's a?)
Code:
 var active = ajaxFunction();


this inside the function doesn't mean the function. it's the owner of the function (I think) so this will not work
Code:
     ct = setInterval(this,100);


if the timer is paused shouldn't the end_time be updated too?
Related topics
[Community Project] Easy Simple Content Management System
Need a comment on the feel of my advertisements
Why Javascript inside php that opened with ajax doesn;t refr
Codeur php/sql/js? j'ai besoin d'un coup de pouce
AJaX or ASPX?
mod_security question
Ajax Inbox
AJAX tutorials?
www.newgengamers.com
Los 50 mejores sitios diseñados en CSS
Qui peut m'aider à créer un site ?
PHP + Ajax
Javascript vs PHP (or python?) - unicode text file search
Need some help.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.