从 Javascript 计时器到数据库所用的时间 [英] Elapsed Time to database from Javascript timer

查看:34
本文介绍了从 Javascript 计时器到数据库所用的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以计算经过的时间并在用户点击停止按钮后以秒为单位打印值.我们的第一个问题是我们试图让它以小时为单位发布经过的时间.所以假设经过的时间是 20 分钟,输出将是 0.3 小时.

I have the following code that will count the elapsed time and print the value in seconds after the user hits the stop button. Our first problem is that we are trying to get it to post the elapsed time in hours. So lets say the elapsed time is 20 mintues, the output would be .3 hours.

其次,我们试图将此值发布到我们创建的 MySQL 数据库内的表中.它在我们的表中是一个 int 值,只是不知道如何将其发布在那里.任何帮助表示赞赏!

Second, we are trying to get this value posted into a table inside of our created MySQL database. Its a int value in our table, just don't know how to post it there. Any help is appreciated!

<script type="text/javascript">

// Javascript to compute elapsed time between "Start" and "Finish" button clicks
function timestamp_class(this_current_time, this_start_time, this_end_time, this_time_difference) { 
        this.this_current_time = this_current_time;
        this.this_start_time = this_start_time;
        this.this_end_time = this_end_time;
        this.this_time_difference = this_time_difference;
        this.GetCurrentTime = GetCurrentTime;
        this.StartTiming = StartTiming;
        this.EndTiming = EndTiming;
    }

    //Get current time from date timestamp
    function GetCurrentTime() {
    var my_current_timestamp;
        my_current_timestamp = new Date();              //stamp current date & time
        return my_current_timestamp.getTime();
        }

    //Stamp current time as start time and reset display textbox
    function StartTiming() {
        this.this_start_time = GetCurrentTime();        //stamp current time
        document.TimeDisplayForm.TimeDisplayBox.value = 0;      //init textbox display to zero
        }

    //Stamp current time as stop time, compute elapsed time difference and display in textbox
    function EndTiming() {
        this.this_end_time = GetCurrentTime();          //stamp current time
        this.this_time_difference = (this.this_end_time - this.this_start_time) / 1000; //compute elapsed time
        document.TimeDisplayForm.TimeDisplayBox.value = this.this_time_difference;      //set elapsed time in display box
        }

var time_object = new timestamp_class(0, 0, 0, 0);  //create new time object and initialize it

//-->
</script>

    <form>
        <input type="button" value="Start" onClick="time_object.StartTiming()"; name="StartButton">
    </form>
    <form>
        <input type="button" value="Finish" onClick="time_object.EndTiming()"; name="EndButton">
    </form>
    <form name="TimeDisplayForm">
    Elapsed time:
      <input type="text" name="TimeDisplayBox" size="6">
    seconds
    </form>

推荐答案

  1. Date.getTime() 以毫秒为单位返回时间,因此 this_time_difference 将具有以毫秒为单位的时间差.您需要做的就是除以 36,00,000(1 小时 = 60 分钟 * 60 秒 * 1000 毫秒)以获得以小时为单位的值.

  1. Date.getTime() returns the time in milliseconds so this_time_difference will have the time difference in milliseconds. All you need to do is divide by 36,00,000 (1 hour = 60 mins * 60 secs * 1000 milliseconds) to get the value in hours.

var timeInHours = this_time_difference/3600000;

  • 我不确定您只是发布这些数据有什么问题(使用 Ajax普通旧表单提交) 到插入值的 PHP 脚本进入您的数据库 - 有关您陷入困境的更多信息将帮助您获得更具体的答案.

  • I'm not sure what problem you have simply POSTing this data (using either Ajax or a plain old form submit) to a PHP script that inserts the value into your DB - more information about where you're stuck will help you get more specific answers.

    这篇关于从 Javascript 计时器到数据库所用的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆