douhui7136 2015-02-20 05:21
浏览 40
已采纳

检查jquery中是否有其他条件

I have jquery which print the remaining time of quiz to my page. It works fine. Now I want to put condition if else to output of timer_check.php's output. I have put condition if remaining time = 0 then echo timeout else print remaining time. My jquery is in quiz.php page. I want to check if output come from timer_check.php is timeout then All answer should submit to database else print remaining time in tableHolder div. So my question is how to put if else condition for output come from timer_check.php to quiz.php to check submit the quiz or print remaining time?

$(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
"));
}
  • 写回答

1条回答 默认 最新

  • dsjpqpdm620596 2015-02-20 05:56
    关注

    Rather than using the load function you could try using an ajax function.Hope this might give you some idea mate.. :)

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

    FYI

    ajax()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站