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条)

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析