簡體   English   中英

檢查jQuery中是否存在其他條件

[英]check if else condition in jquery

我有一個jQuery,它將剩余的測驗時間打印到我的頁面上。 工作正常。 現在,我想將條件設置為timer_check.php的輸出。 我已經把剩余時間= 0設置為條件,然后回顯超時,否則打印剩余時間。 我的jQuery在quiz.php頁面中。 我想檢查是否來自timer_check.php的輸出超時,然后所有答案都應提交到數據庫,否則在tableHolder div中打印剩余時間。 所以我的問題是如何將輸出的其他條件從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"));
}

而不是使用load函數,您可以嘗試使用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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM