检查jquery中的if-else条件


check if else condition in jquery

我有jquery,它将测验的剩余时间打印到我的页面上。它运行良好。现在我想把条件if else放到timer_check.php的输出中。我已经设置了条件,如果剩余时间=0,则回显超时,否则打印剩余时间。我的jquery在quiz.php页面中。我想检查来自timer_check.php的输出是否超时,那么所有答案都应该提交到tableHolderdiv中的数据库else打印剩余时间。所以我的问题是,如何将来自timer_check.php的输出的if else条件设置为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函数。希望这能给你一些想法,伙计…:)

    $(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()