我需要帮助使用计时器自动提交测验

时间:2015-05-07 15:06:14

标签: javascript php html

大家好,我有一个测验系统,我希望测验会在时间结束后自动提交,这是我的代码,但它不起作用。

<script type="text/javascript">
 function countDown(secs, elem)
 {
      var element = document.getElementById(elem);
      element.innerHTML = "<h2>You have <b>"+secs+"</b> seconds to answer the questions</h2>";
      if(secs < 1) {
           document.quiz.submit();
      }
      else
      {
           secs--;
           setTimeout('countDown('+secs+',"'+elem+'")',1500);
      }
 }

 function validate() {
      return true;
 }
 </script> 
 <div id="status"></div>
 <script type="text/javascript">countDown(4,"status");</script>
 <title>Questionnaire</title>
 <style type="text/css"> 
 span { 
    color: #FF00CC;
 }
 </style>



<body>
<br />

<div class="col-sm-8">
<div class="panel panel-black">
<?php
include("db_conn.php");


$query="select * from question";

$rs=mysql_query("select * from question where test_id=$tid",$cn) or die(mysql_error());
if(!isset($_SESSION[qn]))
{
    $_SESSION[qn]=0;
    mysql_query("delete from useranswer where sess_id='" . session_id() ."'") or die(mysql_error());
    $_SESSION[trueans]=0;

}
else
{   
        if($submit=='Next Question' && isset($ans))
        {
                mysql_data_seek($rs,$_SESSION[qn]);
                $row= mysql_fetch_row($rs); 
                mysql_query("insert into useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());
                if($ans==$row[7])
                {
                            $_SESSION[trueans]=$_SESSION[trueans]+1;
                }
                $_SESSION[qn]=$_SESSION[qn]+1;
        }
        else if($submit=='Get Result' && isset($ans))
        {
                mysql_data_seek($rs,$_SESSION[qn]);
                $row= mysql_fetch_row($rs); 
                mysql_query("insert into useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());
                if($ans==$row[7])
                {
                            $_SESSION[trueans]=$_SESSION[trueans]+1;
                }
                echo "<h1 class=head1> Result</h1>";
                $_SESSION[qn]=$_SESSION[qn]+1;
                echo "<Table align=center><tr class=tot><td>Total Question<td> $_SESSION[qn]";
                echo "<tr class=tans><td>True Answer<td>".$_SESSION[trueans];
                $w=$_SESSION[qn]-$_SESSION[trueans];
                echo "<tr class=fans><td>Wrong Answer<td> ". $w;
                echo "</table>";
                mysql_query("insert into result(login,test_id,test_date,score) values('$login',$tid,'".date("d/m/Y")."',$_SESSION[trueans])") or die(mysql_error());

                unset($_SESSION[qn]);
                unset($_SESSION[sid]);
                unset($_SESSION[tid]);
                unset($_SESSION[trueans]);
                exit;
        }
}
$rs=mysql_query("select * from question where test_id=$tid",$cn) or die(mysql_error());
if($_SESSION[qn]>mysql_num_rows($rs)-1)
{
unset($_SESSION[qn]);
echo "<h1 class=head1>Some Error  Occured</h1>";
session_destroy();
echo "Please <a href=test.php> Start Again</a>";

exit;
}
mysql_data_seek($rs,$_SESSION[qn]);
$row= mysql_fetch_row($rs);
echo "<form name=quiz method=post action=quiz.php onsubmit=return validate()>";
echo "<table width=100%> <tr> <td width=30>&nbsp;<td> <table border=0>";
$n=$_SESSION[qn]+1;
echo "<tR><td><span class=style2>Que ".  $n .": $row[2]</style>";
echo "<tr><td class=style8><input type=radio name=ans value=1>$row[3]";
echo "<tr><td class=style8> <input type=radio name=ans value=2>$row[4]";
echo "<tr><td class=style8><input type=radio name=ans value=3>$row[5]";
echo "<tr><td class=style8><input type=radio name=ans value=4>$row[6]";

if($_SESSION[qn]<mysql_num_rows($rs)-1)
echo "<tr><td><input type=submit name=submit class='btn btn-lg btn-black' value='Next Question'></form>";
else
echo "<tr><td><input type=submit name=submit class='btn btn-lg btn-black' value='Get Result'></form>";
echo "</table></table>";
?>
</div>
</div>

此代码我使用的是男士,但问题是自动计时器在超过

之后没有提交测验

1 个答案:

答案 0 :(得分:0)

使用setInterval代替setTimeout。因为如果你使用setTimeout,你的countDown函数只会触发一次。

setInterval(function() {
    countDown(secs, elem)
},1500);