所以我需要做的是在我創建的html頁面上有一個開始時間和結束時間。這是技術人員在他開始工作之後就可以開始工作的時間,當他在那天結束時他會停下來計算總時間。現在我已經找到了一堆能做到這一點的代碼,但我需要更進一步。我需要花費時間並通過我的PHP文件發佈它,以便將其保存在我們的MySQL數據庫中創建的表中。Javascript秒錶到MySQL數據庫
<script type="text/javascript">
var duration = 0;
// 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
//-->
function assignDuration()
{
document.stopform.duration.value = duration;
}
</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="stopform" action="process-form.php" method="POST">
<input type="hidden" name="duration" value="0"/>
<input type="submit" name="dostop" onClick="assignDuration()" value="Stop"/>
</form>
提前致謝!
爲什麼不單單存儲啓動和停止時間?然後,您可以計算經過的時間,並且不允許客戶弄亂事情。 – 2011-04-19 00:37:38
你不想用這種方法測量持續時間。 setTimeout不應該是100%準確的,這取決於系統負載+瀏覽器最終可能會有幾個百分點的折扣,如果你整天運行它,這可能會很重要。通過存儲開始和停止時間並獲取差異來計算持續時間。 – Nathan 2011-04-19 03:09:52