Php计时器不会在00:00

时间:2018-03-06 13:36:37

标签: javascript php mysql ajax

我使用js ajax和php做了一个倒数计时器。 问题是当计时器到达00:00时它不执行指定的操作。而计时器从59:59开始。 这是代码:

    <span class="pull-right" id="time_left"></span>

    date_default_timezone_set('Asia/Karachi');
    echo $_SESSION['end_time'] = $quiz_end_time;

//timer function
        setInterval (function(){
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET","timer.php",false);
            xmlhttp.send(null);
            document.getElementById("time_left").innerHTML = xmlhttp.responseText;
        }, 1000);

//timer.php code
<?php session_start();
    $from_time = date('Y-m-d H:i:s');
    $to_time = $_SESSION['end_time'];

    $time_form = strtotime($from_time);
    $time_to = strtotime($to_time);

    $difference = $time_to - $time_form;

    echo "Time Left: ";echo $timer_time = date("i:s",$difference);echo "<br>"; 
    //echo date ('H:i:s',strtotime($timer_time));

    if($timer_time == "00:00"){
        ?>
        <script>
            document.getElementById("quiz_form").submit();
            //window.location="manage-quizzes.php?Msg=quiz_ended"
        </script>
        <?php
        session_destroy($_SESSION['quiz_id']);
        session_destroy($_SESSION['end_time']);
        header('Location:manage-quizzes.php?Msg=quiz_ended');
    }else{
        echo "OKAY";
    }
?>

1 个答案:

答案 0 :(得分:0)

我们无法在AJAX文件中提交表单。试试这个。

//timer function
setInterval (function(){
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET","timer.php",false);
    xmlhttp.send(null);
    if (xhttp.response != "SUBMIT")
        document.getElementById("time_left").innerHTML = xmlhttp.responseText;
    } else {
        document.getElementById("quiz_form").submit();
    }
}, 1000);

//timer.php code
if ($timer_time == "00:00") {
    echo "SUBMIT";
} else {
    echo "OKAY";
}
相关问题