我已经使用启用了JavaScript的倒数计时器为考试门户网站编写了php代码。当我在本地主机中运行我的代码时。但是在服务器中,时钟会在一段时间后停止,这可能是什么问题?以下是相关代码的一部分。谢谢!!! 在portal.php中
<?php
$_SESSION['exam_length'] = $all_questions[0]->exam_length;
$_SESSION['start_time'] = date('Y-m-d H:i:s');
$end_time = date('Y-m-d H:i:s',strtotime('+'.$_SESSION['exam_length'].'minutes',strtotime($_SESSION['start_time'])));
$_SESSION['end_time'] = $end_time;
?>
<div class="col-md-3 col-lg-3 col-sm-3" id="response" style="font-size: 25px; position: fixed; right: -7px;"></div>
<script type="text/javascript">
setInterval(function()
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","response.php",false);
xmlhttp.send(null);
document.getElementById('response').innerHTML=xmlhttp.responseText;
},1000);
exam_length = '<?php echo (($all_questions[0]->exam_length)*60000); ?>';
var auto_refresh = setInterval(function() { submitform(); },exam_length);
</script>
在response.php中:
<?php
session_start();
if(($_SESSION['is_logged_in'])=="")
{
@header("location:../");
exit;
}
$from_time1 = date('Y-m-d H:i:s');
$to_time1 = $_SESSION['end_time'];
$timefirst = strtotime($from_time1);
$timesecond = strtotime($to_time1);
$difference_second = $timesecond - $timefirst;
echo gmdate("H:i:s",$difference_second);
?>