检查 jquery 中的其他条件

check if else condition in jquery

我有 jquery 将测验的剩余时间打印到我的页面。它工作正常。现在我想把条件 if else 放到 timer_check.php 的输出中。如果剩余时间 = 0 然后回显超时,我已经设置了条件,否则打印剩余时间。我的 jquery 在 quiz.php 页面中。我想检查来自 timer_check.php 的输出是否超时,然后所有答案都应提交给数据库,否则在 tableHolder div 中打印剩余时间。所以我的问题是如何设置输出的 if else 条件从 timer_check.php 到 quiz.php 来检查提交测验或打印剩余时间?

$(document).ready(function() {
  refreshTable();
});

function refreshTable(){
    $('#tableHolder').load('timer_check.php', function(){
       setTimeout(refreshTable, 1000);
    });
}

timer_check.php

session_start();
include("database.php");
$sql = "select * from ctip_test where test_id = '$_SESSION[testid]'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    $starttime = $row['attemp_time'];
}
$datetime1 = new DateTime($starttime);
$datetime1->add(new DateInterval('P0Y0M0DT2H0M0S'));
$currenttime =date('Y-m-d H:i:s');
$datetime2 = new DateTime($currenttime);
$diff = $datetime2->diff($datetime1);
$check_timeout = (array) $datetime2->diff($datetime1);
$hour = $check_timeout['h'];
$minute = $check_timeout['i'];
$second = $check_timeout['s'];
if($hour=="0" && $minute =="0" && $second =="0"){
    echo "time out";
}
else{
  echo($diff->format("%h hours %i minutes and %s seconds are remaining\n"));
}

你可以尝试使用 ajax function.Hope 而不是使用加载函数,这可能会给你一些想法伙计..:)

    $(document).ready(function() {
        refreshTable();
    });

    function refreshTable(){
        $.ajax({
                    url: "timer_check.php",
                    success: function (result) {
                        if(result == "time out"){
                            $("#myId").submit(); // myId is the id of the form
                        }else{
                            $('#tableHolder').html(result);
                           setTimeout(refreshTable, 1000);
                        }
                    }
                });
    }

仅供参考

ajax()