javascript - 将 Java 脚本毫秒计时器值传递给 PHP

标签 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>

最佳答案

我已经为您构建了一个具有最佳性能和最简单处理的 JS fiddle:

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
});

关于javascript - 将 Java 脚本毫秒计时器值传递给 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25591415/

相关文章:

JavaScript 表单验证 : understanding function call

php - 实现facebook风格的 "unlike"函数

php - MySQL更新重复事件

javascript - 为什么我的图表没有更新?

javascript - 当客户端可以是 Javascript 客户端时的 WCF 服务安全性

javascript - html 文件无法从我的模板文件夹中运行

php - Laravel 中 Twitter Bootstrap 导航的自动事件类

php - 可点击的 HTML 行将变量 POST 到不同的 PHP 脚本

java - PHP、AJAX 和 Java

javascript - jQuery sticky header 在特定高度跳转