0

我正在为我的项目创建一个在线考试页面。我有一个倒数计时器,但它会在页面刷新时重置。我怎样才能让它不重置?通过从 db 获取时间来设置计时器。我正在使用 php mysql。请帮我。这是我的代码。

<?php
   $result=mysql_query("select * from test where testid='29'");
   while($time=mysql_fetch_array($result)){
      $dateFormat  = "d F Y -- g:i a";
      $targetDate  = time() + ($time['duration']*60);
      $actualDate  = time();
      $secondsDiff = $targetDate - $actualDate;
      $remainingDay      = floor($secondsDiff/60/60/24);
      $remainingHour     = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
      $remainingMinutes  = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60);
      $remainingSeconds  = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60));
      $actualDateDisplay = date($dateFormat,$actualDate);
      $targetDateDisplay = date($dateFormat,$targetDate);
   }
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Untitled Document</title>
   <script type="text/javascript">
      var days = <?php echo $remainingDay; ?>  
      var hours = <?php echo $remainingHour; ?>  
      var minutes = <?php echo $remainingMinutes; ?>  
      var seconds = <?php echo $remainingSeconds; ?> 
      function setCountDown ()
      {
          seconds--;
          if (seconds < 0){
             minutes--;
             seconds = 59
          }
          if (minutes < 0){
              hours--;
              minutes = 59
          }
          if (hours < 0){
              hours = 23
          }
          document.getElementById("remain").innerHTML = "  "+hours+" hr "+minutes+" min    "+seconds+" sec";
          SD=window.setTimeout( "setCountDown()", 1000 );
          if (minutes == '00' && seconds == '00') { 
              seconds = "00"; window.clearTimeout(SD);
              window.location = "result.php"
          } 

       }
    </script>

</head>
<body onLoad="setCountDown();">
   <div id="remain">
         <?php echo "$remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds";?>
   </div>
4

4 回答 4

2

您应该将值读入会话变量并在之后使用它:

<?php
session_start();
if (isset($_SESSION['targetdate'])) {
    // session variable_exists, use that
    $targetDate = $_SESSION['targetdate'];
} else {
    // No session variable, red from mysql
    $result=mysql_query("select * from test where testid='29' LIMIT 1");
    $time=mysql_fetch_array($result);
    $dateFormat = "d F Y -- g:i a";
    $targetDate = time() + ($time['duration']*60);
    $_SESSION['targetdate'] = $targetDate;
}

$actualDate = time();
$secondsDiff = $targetDate - $actualDate;
$remainingDay     = floor($secondsDiff/60/60/24);
$remainingHour    = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
$remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-         ($remainingHour*60*60))/60);
$remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-    ($remainingHour*60*60))-($remainingMinutes*60));
$actualDateDisplay = date($dateFormat,$actualDate);
$targetDateDisplay = date($dateFormat,$targetDate);

?>
于 2012-06-30T07:11:58.763 回答
1

当用户开始考试时,将开始时间保存在数据库中。将它用于一切。不要依赖客户端代码,因为用户可以轻松修改它。

如果您保存开始时间,您就知道用户何时开始以及当前时间是什么。您可以使用它来查看时间是否到了。javascript 计时器可以很好地显示剩余时间,但它应该从数据库中获取该信息。

于 2012-06-30T07:08:58.990 回答
1

您可能需要使用examId 字段和datetimeStarted 注册像active_exams 这样的表。然后将计时器加载为该考试自开始以来的秒数。

您可以使用 cookie、localStorage 甚至会话创建一些解决方法,但请记住,用户可以修改(或者至少在会话的情况下可以忘记)。

于 2012-06-30T07:09:38.723 回答
1

如果您在数据库中为特定用户保存了计时​​器的“开始时间”,则页面刷新并不重要。

从数据库中获取这个“开始时间”并计算它以获得消耗的时间,并相应地显示剩余或经过的时间。

于 2013-07-04T06:47:35.667 回答