将Javascript毫秒计时器值传递给PHP

时间:2014-08-31 11:13:15

标签: javascript php

我使用了Java Scipt毫秒计时器,我想在用户提交窗口时将计时器值传递给php。任何人都可以请求如何将JS计时器值发送给PHP?

我的JS计时器代码如下: -

当用户提交表单时,我需要将Seconds.Millisecond值传递给PHP。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">

    $(function () {
        $(document).keydown(function (e) {
            return (e.which || e.keyCode) != 116;
        });
    });
</script>
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {
      $(document).bind('contextmenu',function(e){
        e.preventDefault();
        alert('Right Click is not allowed');
      });
       });
</script>
<script>
var millisec = 0;
var seconds = 0;
var timer;
function display(){

  if (millisec>=99){
     millisec=0
     seconds+=1
  }
  else
     millisec+=1
     document.d.d2.value = seconds + "." + millisec;
     timer = setTimeout("display()",8);
     }
function startstoptimer() {
  if (timer > 0) {
     clearTimeout(timer);
     timer = 0;
  } else {
     display();
  }
}

</script>
<b>Time Elapsed</b>
<form name="d">
<input type="text" id="time" size="8" name="d2">
</form>

        <script>
            function logout() 
            {
                alert('Times Up, Thank you for taking part in this Quiz!');
                document.forms['QuizQuestions'].submit();
            }

            function StartTimer() 
            {
                t = setTimeout(logout, 80000)//logs out in 15 mins

            }
            function submitThis() {
                document.forms['QuizQuestions'].submit();
            }
        </script>

        </script>

        <body bgcolor="#cceeff" onLoad="startstoptimer();">
        Hello <?php session_start();
        echo $_SESSION['signum'];
            $my_t=getdate(date("U"));
            $datestamp = "$my_t[weekday], $my_t[month] $my_t[mday], $my_t[year], $my_t[hours]:$my_t[minutes]:$my_t[seconds]";
            $_SESSION['StartTime'] = $datestamp;
            $_SESSION['timervals']="<script> document.write(c)</script>";
        ?>, All the best !
        <form name='QuizQuestions' method="post" action="ScoreQuiz.php">
            <pre>
            <font face='sans-serif' size=2>
<input type="hidden" name="time1"  id="time">

Question 1
In the following PHP Script, what is the correct output:
$x=array("aaa","ttt","www","ttt","yyy","tttt");
$y=array_count_values($x);
echo $y[ttt];
<input type="hidden" name="qq1" value="null">
<input type="radio" name="qq1" value="1"> 2
<input type="radio" name="qq1" value="2"> 3
<input type="radio" name="qq1" value="3"> 1
<input type="radio" name="qq1" value="4"> 4

<hr>Question 2
In PHP Which method is used to getting browser properties?
<input type="hidden" name="qq2" value="null">
<input type="radio" name="qq2" value="1"> $_SERVER['HTTP_USER_AGENT']
<input type="radio" name="qq2" value="2"> $_SERVER['PHP_SELF']
<input type="radio" name="qq2" value="3"> $_SERVER['SERVER_NAME']
<input type="radio" name="qq2" value="4"> $_SERVER['HTTP_VARIENT']

<hr>Question 3
In the following PHP Script, what is the correct output:
$x=dir(".");
while($y=$x->read())
{
echo $y."
"
}
$y->close();
<input type="hidden" name="qq3" value="null">
<input type="radio" name="qq3" value="1"> display all folder names
<input type="radio" name="qq3" value="2"> display a folder content
<input type="radio" name="qq3" value="3"> display content of the all drives
<input type="radio" name="qq3" value="4"> Parse error

<hr>Question 4
In PHP, which of the following function is used to insert content of 
one php file into another php file before server executes it:
<input type="hidden" name="qq4" value="null">
<input type="radio" name="qq4" value="1"> include[]
<input type="radio" name="qq4" value="2"> #include()
<input type="radio" name="qq4" value="3"> include()
<input type="radio" name="qq4" value="4"> #include{}

<hr>Question 5
In PHP the error control operator is _______
<input type="hidden" name="qq5" value="null">
<input type="radio" name="qq5" value="1"> .
<input type="radio" name="qq5" value="2"> *
<input type="radio" name="qq5" value="3"> @
<input type="radio" name="qq5" value="4"> &

<hr></font>
</pre>
<input type="submit" value="Submit">
</form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

我为您提供了最佳性能和最简单处理的JS小提琴:

http://jsfiddle.net/ojzw2hfu/

将隐藏的输入放入表单中,设置间隔函数以更新输入并在提交时停止计时器。该值包括微秒,但我会留给您找到一种方法来减少它; - )

var startTime;
var endTime;
var timer;


$(document).ready(function() {
    startTime = performance.now();

    timer = window.setInterval(function() {
        endTime = performance.now();
        $('#timer').text(endTime - startTime);
    });

});

$('#myForm').on('submit', function() {
    window.clearInterval(timer);
    $('#millisecondValueInForm').val(endTime - startTime);
    return false; //remove this later in real script because it prevents form from submitting
});