Elapsed Time to database from Javascript timer

2019-08-29 12:59发布

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.

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条回答
Bombasti
2楼-- · 2019-08-29 13:23
  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;
    
  2. 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.

查看更多
登录 后发表回答