【问题标题】:Elapsed Time to database from Javascript timer从 Javascript 计时器到数据库的经过时间
【发布时间】:2011-04-25 01:24:06
【问题描述】:

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

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

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

【问题讨论】:

    标签: php javascript mysql timer


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

      var timeInHours = this_time_difference/3600000;
      
    2. 我不确定您只是将这些数据(使用 Ajaxplain old form submit)发布到将值插入数据库的 PHP 脚本中遇到了什么问题 - 有关您所在位置的更多信息卡住将帮助您获得更具体的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 2017-12-13
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多