[英]timer opens and closes immediately by submitting questions without allowing students to attempt examination
請允許任何人幫忙,因為我的電子考試中的倒數計時器剛停止工作,我無法弄清楚實際發生了什么...計時器將讀取零(hh:min:sec)並自動提交學生問題而不允許他們回答問題,從而將他們(學生)評為零。 謝謝...
function countDown()
{
sec--;
if (sec == -01) {
sec = 59;
min = min - 1;
if(min==-01)
{
min=59;
hour=hour-1;
}else
{
hour=hour;
}
} else {
min = min;
}
if (sec<=9) { sec = "0" + sec; }
time = (hour<=9 ? "0" + hour : hour) + " : " + (min<=9 ? "0" + min : min) + " : " + sec + "";
if (document.getElementById) { document.getElementById('timer').innerHTML = time; }
SD=window.setTimeout("countDown();", 1000);
if (hour=='00'&& min == '00' && sec == '00') { sec = "00";min="00"; window.clearTimeout(SD); window.location=document.forms[0].action+"?fs=yes";}
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
countDown();
});
腳本的php部分是這樣的:
<?php
error_reporting(0);
session_start();
include_once 'oesdb.php';
$final=false;
if(!isset($_SESSION['stdname'])) {
$_GLOBALS['message']="Session Timeout.Click here to <a href=\"index.php\">Re-LogIn</a>";
}
else if(isset($_REQUEST['logout']))
{
//Log out and redirect login page
unset($_SESSION['stdname']);
header('Location: index.php');
}
else if(isset($_REQUEST['dashboard'])){
//redirect to dashboard
//
header('Location: stdwelcome.php');
}else if(isset($_REQUEST['next']) || isset($_REQUEST['summary']) || isset($_REQUEST['viewsummary']))
{
//next question
$answer='unanswered';
if(time()<strtotime($_SESSION['endtime']))
{
if(isset($_REQUEST['markreview']))
{
$answer='review';
}
else if(isset($_REQUEST['answer']))
{
$answer='answered';
}
else
{
$answer='unanswered';
}
if(strcmp($answer,"unanswered")!=0)
{
if(strcmp($answer,"answered")==0)
{
$query="update studentquestion set answered='answered',stdanswer='".htmlspecialchars($_REQUEST['answer'],ENT_QUOTES)."' where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";";
}
else
{
$query="update studentquestion set answered='review',stdanswer='".htmlspecialchars($_REQUEST['answer'],ENT_QUOTES)."' where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";";
}
if(!executeQuery($query))
{
// to do
$_GLOBALS['message']="Your previous answer is not updated.Please answer once again";
}
closedb();
}
if(isset($_REQUEST['viewsummary']))
{
header('Location: summary.php');
}
if(isset($_REQUEST['summary']))
{
//summary page
header('Location: summary.php');
}
}
if((int)$_SESSION['qn']<(int)$_SESSION['tqn'])
{
$_SESSION['qn']=$_SESSION['qn']+1;
}
if((int)$_SESSION['qn']==(int)$_SESSION['tqn'])
{
$final=true;
}
}
else if(isset($_REQUEST['previous']))
{
// Perform the changes for current question
$answer='unanswered';
if(time()<strtotime($_SESSION['endtime']))
{
if(isset($_REQUEST['markreview']))
{
$answer='review';
}
else if(isset($_REQUEST['answer']))
{
$answer='answered';
}
else
{
$answer='unanswered';
}
if(strcmp($answer,"unanswered")!=0)
{
if(strcmp($answer,"answered")==0)
{
$query="update studentquestion set answered='answered',stdanswer='".htmlspecialchars($_REQUEST['answer'],ENT_QUOTES)."' where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";";
}
else
{
$query="update studentquestion set answered='review',stdanswer='".htmlspecialchars($_REQUEST['answer'],ENT_QUOTES)."' where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";";
}
if(!executeQuery($query))
{
// to do
$_GLOBALS['message']="Your previous answer is not updated.Please answer once again";
}
closedb();
}
}
//previous question
if((int)$_SESSION['qn']>1)
{
$_SESSION['qn']=$_SESSION['qn']-1;
}
}
else if(isset($_REQUEST['fs']))
{
//Final Submission
header('Location: testack.php');
}
?>
<?php
header("Cache-Control: no-cache, must-revalidate");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Examination Platform
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE"/>
<meta http-equiv="PRAGMA" content="NO-CACHE"/>
<meta name="ROBOTS" content="NONE"/>
<link rel="stylesheet" type="text/css" href="oes.css"/>
<script type="text/javascript" src="validate.js" ></script>
<script type="text/javascript" src="cdtimer.js" ></script>
<script type="text/javascript" >
<!--
<?php
$elapsed=time()-strtotime($_SESSION['starttime']);
if(((int)$elapsed/60)<(int)$_SESSION['duration'])
{
$result=executeQuery("select TIME_FORMAT(TIMEDIFF(endtime,CURRENT_TIMESTAMP),'%H') as hour,TIME_FORMAT(TIMEDIFF(endtime,CURRENT_TIMESTAMP),'%i') as min,TIME_FORMAT(TIMEDIFF(endtime,CURRENT_TIMESTAMP),'%s') as sec from studenttest where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid'].";");
if($rslt=mysql_fetch_array($result))
{
echo "var hour=".$rslt['hour'].";";
echo "var min=".$rslt['min'].";";
echo "var sec=".$rslt['sec'].";";
}
else
{
$_GLOBALS['message']="Try Again";
}
closedb();
}
else
{
echo "var sec=01;var min=00;var hour=00;";
}
?>
-->
</script>
</head>
<body >
<noscript><h2>For the proper Functionality, You must use Javascript enabled Browser</h2></noscript>
<?php
if($_GLOBALS['message']) {
echo "<div class=\"message\">".$_GLOBALS['message']."</div>";
}
?>
<div id="container">
<div class="header">
<img style="margin:10px 2px 2px 10px;float:left;" height="80" width="200" src="images/logo.gif" alt=""/><h3 class="headtext"> eExamination </h3><h4 style="color:#ffffff;text-align:center;margin:0 0 5px 5px;"><i>Idea | Innovation | Motivation</i></h4>
</div>
<form id="testconducter" action="testconducter.php" method="post">
<div class="menubar" style="text-align:center;">
<h2 style="font-family:helvetica,sans-serif;font-weight:bolder;font-size:120%;color:#f50000;padding-top:0.3em;letter-spacing:1px;">Examination Platform</h2>
</div>
<div class="page">
<?php
if(isset($_SESSION['stdname']))
{
$result=executeQuery("select stdanswer,answered from studentquestion where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";");
$r1=mysql_fetch_array($result);
$result=executeQuery("select * from question where testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";");
$r=mysql_fetch_array($result);
?>
<div class="tc">
<table border="0" width="100%" class="ntab">
<tr>
<th style="width:40%;"><h3><span id="timer" class="timerclass"></span></h3></th>
<th style="width:40%;"><h4 style="color: #af0a36;">Question No: 1 </h4></th>
<th style="width:20%;"><h4 style="color: #af0a36;"><input type="checkbox" name="markreview" value="mark"> Mark for Review</input></h4></th>
</tr>
</table>
<textarea cols="100" rows="8" name="question" readonly style="width:96.8%;text-align:left;margin-left:2%;margin-top:2px;font-size:120%;font-weight:bold;margin-bottom:0;color:#0000ff;padding:2px 2px 2px 2px;"><?php echo htmlspecialchars_decode($r['question'],ENT_QUOTES); ?></textarea>
<table border="0" width="100%" class="ntab">
<tr><td> </td></tr>
<tr><td >1. <input type="radio" name="answer" value="optiona" <?php if((strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"review")==0 ||strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"answered")==0)&& strcmp(htmlspecialchars_decode($r1['stdanswer'],ENT_QUOTES),"optiona")==0 ){echo "checked";} ?>> <?php echo htmlspecialchars_decode($r['optiona'],ENT_QUOTES); ?></input></td></tr>
<tr><td >2. <input type="radio" name="answer" value="optionb" <?php if((strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"review")==0 ||strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"answered")==0)&& strcmp(htmlspecialchars_decode($r1['stdanswer'],ENT_QUOTES),"optionb")==0 ){echo "checked";} ?>> <?php echo htmlspecialchars_decode($r['optionb'],ENT_QUOTES); ?></input></td></tr>
<tr><td >3. <input type="radio" name="answer" value="optionc" <?php if((strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"review")==0 ||strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"answered")==0)&& strcmp(htmlspecialchars_decode($r1['stdanswer'],ENT_QUOTES),"optionc")==0 ){echo "checked";} ?>> <?php echo htmlspecialchars_decode($r['optionc'],ENT_QUOTES); ?></input></td></tr>
<tr><td >4. <input type="radio" name="answer" value="optiond" <?php if((strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"review")==0 ||strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"answered")==0)&& strcmp(htmlspecialchars_decode($r1['stdanswer'],ENT_QUOTES),"optiond")==0 ){echo "checked";} ?>> <?php echo htmlspecialchars_decode($r['optiond'],ENT_QUOTES); ?></input></td></tr>
<tr><td> </td></tr>
<tr>
<th style="width:80%;"><h4><input type="submit" name="<?php if($final==true){ echo "viewsummary" ;}else{ echo "next";} ?>" value="<?php if($final==true){ echo "View Summary" ;}else{ echo "Next";} ?>" class="subbtn"/></h4></th>
<th style="width:12%;text-align:right;"><h4><input type="submit" name="previous" value="Previous" class="subbtn"/></h4></th>
<th style="width:8%;text-align:right;"><h4><input type="submit" name="summary" value="Summary" class="subbtn" /></h4></th>
</tr>
</table>
</div>
<?php
closedb();
}
?>
</div>
</form>
<div id="footer">
<p style="font-size:70%;color:#ffffff;"> </b><br/> </p><p></p>
</div>
</div>
</body>
</html>
我嘗試了你的代碼,它返回了sec,min和hour變量的ReferenceError。
您可以嘗試在頂部的sec,min和hour變量上聲明一個值為全局變量,如下面的代碼所示,看看它是否有效:
<html>
<body>
<div id="timer"></div>
<form action="http://stackoverflow.com/">
</form>
<script>
var sec = 0;
var min = 1;
var hour = 0;
function countDown()
{
sec--;
if (sec == -01) {
sec = 59;
min = min - 1;
if (min==-01) {
min=59;
hour=hour-1;
} else {
hour=hour;
}
} else {
min = min;
}
if (sec<=9) { sec = "0" + sec; }
time = (hour<=9 ? "0" + hour : hour) + " : " + (min<=9 ? "0" + min : min) + " : " + sec + "";
if (document.getElementById) { document.getElementById('timer').innerHTML = time; }
SD=window.setTimeout("countDown();", 1000);
if (hour=='00'&& min == '00' && sec == '00') { sec = "00";min="00"; window.clearTimeout(SD); window.location=document.forms[0].action+"?fs=yes";}
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
countDown();
});
</script>
</body>
</html>
如果小時,分鍾和秒變為0,您可以使用setInterval()重復秒倒計時,然后重定向到所需的頁面。 請查看我編寫的示例代碼。 我希望我幫忙。
<html>
<body>
<div id="timer"></div>
<form action="http://stackoverflow.com/"></form>
<script>
function countDown(){
//declare and init variables
var countDown_value = 60, //in seconds
stopFunction = undefined,
countFunction = undefined,
date = undefined,
remainingTime = undefined,
timer = undefined;
//main timer
timer = setInterval(function(){
//stopFunction and redirect
stopFunction = function(){
clearInterval(timer);
//activate below code to redirect
//window.location=document.forms[0].action+"?fs=yes";
document.getElementById('timer').innerHTML = "Done";
}
//countFunction
countFunction = function(){
date = new Date(null);
date.setSeconds(countDown_value);
remainingTime = date.toISOString().substr(11, 8);
document.getElementById('timer').innerHTML = remainingTime;
}
//check if countDown_value is 0
countDown_value-- ? countFunction() : stopFunction();
}, 1000); //1000 -> repeat every second
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
countDown();
});
</script>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.