javascript - 检查 jquery 中的 if else 条件

标签 javascript php jquery ajax if-statement

我有 jquery 可以将测验的剩余时间打印到我的页面上。效果很好。现在我想将条件 if else 放入timer_check.php 的输出中。我设置了条件,如果剩余时间 = 0,则回显超时,否则打印剩余时间。我的 jquery 在 quiz.php 页面中。我想检查来自timer_check.php的输出是否超时,然后所有答案都应该提交到数据库,否则在tableHolder div中打印剩余时间。所以我的问题是如何将来自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()

关于javascript - 检查 jquery 中的 if else 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28622115/

相关文章:

javascript - JS图表旋钮: animate from value to value rather than 0

javascript - 生成表的绝对表头

javascript - 如何在 NVD3.js 中获取轴的更新和渲染值

javascript - 如何加载动态的 JSON 数组?

javascript - 获取按日期过滤的 JSON 数据并从中创建一个新数组

php - mysql_fetch_array 出错

javascript - 在替换的 HTML DOM 上添加事件监听器

javascript - 在 Azure Functions 绑定(bind)中将队列触发器消息引用为 JSON

php - Symfony2 : Retrieve app. 来自命令的 Twig 请求

javascript - Google Adword Api 与 php 集成