그래서 내가하고 싶은 것은 내가 만드는 html 페이지에서 시작 시간과 종료 시간을 갖는 것입니다. 기술자가 작업을 시작하면 기술자가 시계를 시작할 수 있도록하고, 그날이 끝나면 멈추고 경과 된 총 시간을 계산합니다. 이제이 작업을 수행 할 코드를 찾았지만 한 걸음 더 나아가 야합니다. 시간이 경과하고 PHP 파일을 통해 POST해야 MySQL 데이터베이스에 만들어진 테이블에 저장됩니다.자바 스크립트 스톱워치 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>
감사합니다.
시작과 종료 시간을 별도로 저장하지 않는 이유는 무엇입니까? 그런 다음 나중에 경과 시간을 계산할 수 있으며 클라이언트가 사물을 엉망으로 만들지 않도록 할 수 있습니다. –
이런 식으로 측정 지속 시간을 원하지는 않습니다. setTimeout은 시스템로드 + 브라우저에 따라 100 % 정확하지 않아야합니다. 브라우저는 하루 종일 실행하는 경우 중요한 위치가 될 수 있습니다. 시작 및 종료 시간을 저장하고 차이를 가져와 지속 시간을 계산하십시오. – Nathan