douyin6188 2017-02-16 18:02
浏览 54
已采纳

测量两个PHP表单提交之间的时间

I am attempting to measure the period of time from when a user submits a PHP form to when they submit again. The form's action is the same page so effectively it's just a refresh. Moreover, the user may input the same data again. I need it so that it begins counting before the page refreshes as the result must be as accurate as possible. I have already tried a number of methods but none of which resulted in any success. I have simplified the following code:

HTML Form

<form method="GET" action="index.php">
<input id="motion_image" type="image" value="1" name="x" src="img/btn1.png">
<input id="motion_image" type="image" value="2" name="x" src="img/btn2.png">
</form>

Ultimately, I need to have a PHP or JavaScript variable of how long it took a user to press either one of these two buttons to when they again press either one of them. It is important that the counter begins before the refresh as the variable needs to be as accurate as possible. Furthermore, it needs to be responsive so that after say 5 seconds it triggers an event (e.g. a JavaScript alert). I did not feel it was necessary to include my previous attempts as they were all unsuccessful and I believe there is probably a better way. I have full access to the server the site is being hosted on so running a python sub script and exchanging variables using JSON or any other similar solutions are entirely possible.

Apologies for any mistakes and my general lack of stack overflow skills :)

Thanks

  • 写回答

3条回答 默认 最新

  • duanhuilao0787 2017-02-17 14:35
    关注

    After looking into the use of localStorage with JS I was able to find an accurate solution. I simplified my code for others who may face the same or a similar issue. Thanks for all those who helped :)

    HTML

    <form method="GET" action="index.php">
    <input id="motion_image" type="image" value="1" name="x" src="img/btn1.png" onclick="return processInput(1)">
    <input id="motion_image" type="image" value="2" name="x" src="img/btn2.png" onclick="return processInput(2)">
    <input id="motion_image" type="image" value="3" name="x" src="img/btn2.png" onclick="return processInput(3)">
    </form>
    

    JavaScript

    function processInput(x) {
        localStorage.eventType = x;
        checkEventType();
    }
    
    function checkEventType() {
        if (Number(localStorage.eventType) == 1) {
            runningInterval = setInterval(function() {
                localStorage.counter = Number(localStorage.counter) + 1;
            }, 10);
        } else if (Number(localStorage.eventType) == 2) {
            runningInterval = setInterval(function() {
                localStorage.counter = Number(localStorage.counter) - 1;
            }, 10);
        } else if (Number(localStorage.eventType) == 3) {
            window.alert("Your counter is at " + localStorage.counter);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容