如何使用会话变量时间在JavaScript中显示测验计时器

时间:2019-06-22 06:56:02

标签: javascript php

当前我在一个在线测验平台上工作,我想显示一个测验计时器。首先我使用JavaScript来显示计时器,但问题是当用户刷新页面或返回测验计时器时,再次从表单启动开始,所以我想使用会话变量时间显示测验计时器。当用户启动测验时,我设置了该会话变量。但我的代码无法正常工作。这是我的代码:

`<?php
include_once 'dbConnection.php';
session_start();
if(!(isset($_SESSION['email']))){
header("location:login.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
 
 <script>
  // timer will start when document is loaded 
  $(document).ready( function () { 
  startTest();
  });
  var si;
  function timer(){
      <?php $_SESSION['time']-= 1000; ?>
      var count="<?php  echo $_SESSION['time'];  ?>";
      var min = Math.floor(count / (60 * 1000));
      var sec = Math.floor((count - (min * 60 * 1000)) / 1000);
      min = (min < 10)?'0'+min:min;
      sec = (sec < 10)?'0'+sec:sec;   
      if (count <= 0){
        document.getElementById('duration').innerHTML ="Time Up";
        clearInterval(si);
       // submit my quiz.
      } 
      else {
        document.getElementById('duration').innerHTML = "00:" + min + ":" + sec;
      }
    } 
    function startTest(){
      si = setInterval( "timer()", 1000);
    }
</script>
</head>
<body>
  <p id="duration" >show time</p>

</body>
</html>`

此代码给出了一些意外错误或无法正常工作。

2 个答案:

答案 0 :(得分:0)

删除双引号(如果它是整数)

Private Type DocInfo
    pDocName As String
    pOutputFile As String
    pDatatype As String
End Type

Private Declare PtrSafe Function ClosePrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long) As Long
Private Declare PtrSafe Function EndDocPrinter Lib "winspool.drv" (ByVal 
_
   hPrinter As Long) As Long
Private Declare PtrSafe Function EndPagePrinter Lib "winspool.drv" (ByVal 
_
   hPrinter As Long) As Long
Private Declare PtrSafe Function OpenPrinter Lib "winspool.drv" Alias _
   "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
    ByVal pDefault As Long) As Long
Private Declare PtrSafe Function StartDocPrinter Lib "winspool.drv" Alias 
_
   "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
   pDocInfo As DocInfo) As Long
Private Declare PtrSafe Function StartPagePrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long) As Long
Private Declare PtrSafe Function WritePrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, _
   pcWritten As Long) As Long    


Public Function RawPrint(strData As String, ByVal SelectedPrinter As String)
Dim defprt As Printer

Dim lhPrinter As Long
Dim lReturn As Long
Dim lpcWritten As Long
Dim lDoc As Long
Dim sWrittenData As String
Dim mDocInfo As DocInfo
lReturn = OpenPrinter(SelectedPrinter, lhPrinter, 0)

If lReturn = 0 Then
    MsgBox "The Printer is not recognized."
    Exit Function
End If
mDocInfo.pDocName = "ZPl"
mDocInfo.pOutputFile = vbNullString
mDocInfo.pDatatype = vbNullString
lDoc = StartDocPrinter(lhPrinter, 1, mDocInfo)
Call StartPagePrinter(lhPrinter)
sWrittenData = strData
lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, _
Len(sWrittenData), lpcWritten)
lReturn = EndPagePrinter(lhPrinter)
lReturn = EndDocPrinter(lhPrinter)
lReturn = ClosePrinter(lhPrinter)



Exit_RawPrint:
Exit Function



 End Function

答案 1 :(得分:0)

我从您的描述中了解到,您需要服务器端的持续时间。因此,如果有任何刷新浏览器的话,不要从头开始重启计时器。 您需要在服务器端计算持续时间。这可以通过使用ajax调用来实现。

<?php
session_start();
$start_time = $_SESSION['start_time']; // start time;
$from_time =  time(); // current time;


    $minutes = round(abs($start_time - $from_time) / 60);
    $seconds = abs($to_time - $from_time) % 60;

    echo "$minutes minutes & $seconds seconds";

?>

创建用于计算时间timer.php的新php文件

where ⋆\star⋆ is the valid cross-correlation operator,