我正在用 PHP 创建一个定时测试产品。我在正确控制考试时间方面遇到了问题。当考试开始时,只要我在同一个屏幕上,计时器就会正常工作。但是,当我在测试进行时(比如 10 分钟)打开其他屏幕,并且在一段时间后返回测试时,计时器显示错误的时间,并且显示好像只有前 10 分钟消耗的测试时间的 2 分钟。
只是想知道为什么当我在测试开始时离开屏幕并在一段时间后回来时测试计时器的这种不稳定行为。
谁能帮我解决这个问题?
PHP代码
<?php
/*Timer Code*/
$dateFormat = "d F Y -- g:i a";
$targetDate = time() + (240*60);//Change the 240 to however many minutes you want to countdown
$actualDate = time();
$secondsDiff = $targetDate - $actualDate;
$remainingDay = floor($secondsDiff/60/60/24);
$remainingHour = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
$remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60);
$remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60));
$actualDateDisplay = date($dateFormat,$actualDate);
$targetDateDisplay = date($dateFormat,$targetDate);
?>
JavaScript
/*Timer Code*/
var days = <?php echo $remainingDay; ?>
var hours = <?php echo $remainingHour; ?>
var minutes = <?php echo $remainingMinutes; ?>
var seconds = <?php echo $remainingSeconds; ?>
function setCountDown ()
{
seconds--;
if (seconds < 0){
minutes--;
seconds = 59
}
if (minutes < 0){
hours--;
minutes = 59
}
if (hours < 0){
days--;
hours = 23
}
document.getElementById("remain").innerHTML = "Time "+hours+" H : "+minutes+" M : "+seconds+" S";
SD=window.setTimeout( "setCountDown()", 1000 );
if (hours == '0' && minutes == '00' && seconds == '00')
{
seconds = "00"; window.clearTimeout(SD);
timeStatus = 1;
var response = window.alert("Time is up. Press OK to continue."); // change timeout message as required
} } // here is the end of setCountDown