javascript 在线考试系统的计时器,用于存储每个单独部分的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8816158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Timer for online examination system to stores the time for each individual section
提问by Bazooka
I am developing an online examination for which i need to have a countdown timer/clock to be displayed during the entire duration of the test. Also while displaying the final result the time taken to solve each individual questions should also be displayed along with the total time taken for the test.
我正在开发一项在线考试,我需要在整个考试期间显示倒数计时器/时钟。此外,在显示最终结果时,还应显示解决每个单独问题所花费的时间以及测试所花费的总时间。
What are the possible best approaches to implement this functionality??
实现此功能的最佳方法是什么?
<?php
$countfile = "counter.txt";
// location of site statistics.
$statsfile = "stats.txt";
// checks whether the file exist, if not then server will create it.
if (file_exists($countfile)) {
// open the counter hit file.
$fp = fopen($countfile, "r");
// reads the counter hit file and gets the size of the file.
$output = fread($fp, filesize($countfile));
// close the counter hit file.
fclose($fp);
// get the integer value of the variable.
$count = intval($output);
}
// if file is doesn't exist, the server will create the counter hit file and gives a value of zero.
else {
$count = 0;
}
// showcount function starts here.
function ShowCount() {
// declares the global variables.
global $ShowCount, $countfile, $statsfile, $count;
// get the current month.
$month = date('m');
// get the current day.
$day = date('d');
// get the current year.
$year = date('Y');
// get the current hour.
$hour = date('G');
// get the current minute.
$minute = date('i');
// get the current second.
$second = date('s');
// this is the date used in the stats file
$date = "$month/$day/$year $hour:$minute:$second";
// this is the remote IP address of the user.
$remoteip = getenv("REMOTE_ADDR");
// some of the browser details of the user.
$otherinfo = getenv("HTTP_USER_AGENT");
// retrieve the last URL where the user visited before visiting the current file.
$ref = getenv("HTTP_REFERER");
// open the statistics file.
$fp = fopen($statsfile, "a");
// put the given data into the statistics file.
fputs($fp, "Remote Address: $remoteip | ");
fputs($fp, "Information: $otherinfo | ");
fputs($fp, "Date: $date | ");
fputs($fp, "Referer: $ref\n");
// close the statistics file.
fclose($fp);
// adds 1 count to the counter hit file.
$count++;
// open the counter hit file.
$fp = fopen($countfile, "w");
// write at the counter hit file.
// if the value is 34, it will be changed to 35.
fwrite($fp, $count);
// close the counter hit file.
fclose($fp);
// showcount variable is equal to count variable.
$ShowCount = $count;
// return the value of the count variable into showcount variable.
return $ShowCount;
}
// display the value in the counter hits file.
echo showcount(), " visits";
?>
I am using this script to track the visitor stats and time of visit...it is working fine.however I am not able to track the time of individual question in each category of the exam (each exam has multiple categories having multiple questions). Need help
我正在使用此脚本来跟踪访问者统计信息和访问时间......它工作正常。但是我无法跟踪每个考试类别中个别问题的时间(每个考试都有多个类别有多个问题) . 需要帮忙
回答by Prof83
Using a session, you'll need to track when the user's time expires for the section
使用会话,您需要跟踪用户在该部分的时间何时到期
<?php
// Upon starting the section
session_start();
$_SESSION['TIMER'] = time() + 600; // Give the user Ten minutes
?>
Dont do that on page reload though because they can simply refresh the page and reset the timer
不要在页面重新加载时这样做,因为他们可以简单地刷新页面并重置计时器
On the page, use Javascript to display a clock:
在页面上,使用Javascript显示一个时钟:
<script type="text/javascript">
var TimeLimit = new Date('<?php echo date('r', $_SESSION['TIMER']) ?>');
</script>
You can then use the variable 'TimeLimit' to display a countdown
然后,您可以使用变量“TimeLimit”来显示倒计时
<script type="text/javascript">
function countdownto() {
var date = Math.round((TimeLimit-new Date())/1000);
var hours = Math.floor(date/3600);
date = date - (hours*3600);
var mins = Math.floor(date/60);
date = date - (mins*60);
var secs = date;
if (hours<10) hours = '0'+hours;
if (mins<10) mins = '0'+mins;
if (secs<10) secs = '0'+secs;
document.body.innerHTML = hours+':'+mins+':'+secs;
setTimeout("countdownto()",1000);
}
countdownto();
</script>
回答by Amar Palsapure
You can achieve this by doing
你可以通过这样做来实现这一点
- When student starts the exam, store the StartTimein some store/database.
- To display the count down, you can use javascript on client side. Write a function which will take 2 times, first will be the server time and second will be the StartTime. Using these you can find out how much time the candidate is solving problem, and from this you can find out time remaining. Using setIntervalof javascript you can show ticking watch.
- For time taken to solve question, store the duration for which question is visible to the candidate.
- Add the time taken for individual question, and show total time.
- 当学生开始考试时,将StartTime存储在某个存储/数据库中。
- 要显示倒计时,您可以在客户端使用 javascript。编写一个需要 2 次的函数,第一次是服务器时间,第二次是 StartTime。使用这些,您可以了解候选人解决问题的时间,并从中了解剩余的时间。使用javascript 的setInterval您可以显示滴答作响的手表。
- 对于解决问题所花费的时间,存储问题对候选人可见的持续时间。
- 添加单个问题所花费的时间,并显示总时间。
I guess this is the answer you were looking for.
我想这就是你要找的答案。