jQuery-计算JSON元素

I'm trying to list out recently active users via JSON/jQuery. However, this counting system is not working as it should. It both lists out the users and gives a "No active users" message. How could I fix this?

Thanks!

PHP code that grabs the active users:

$liveView_single = array();
$liveView_array = array();

while ($row = mysqli_fetch_assoc($result)){
    extract($row);

    $liveView_single['id'] = $id;
    $liveView_single['type'] = $type;
    $liveView_single['username'] = $username;
    $liveView_single['lastip'] = $lastip;
    $liveView_single['lastactivitypage'] = $lastactivitypage;

    $liveView_full[] = $liveView_single;
}

echo(json_encode($liveView_full));
?>  

jQuery that grabs the JSON elements

<script type="text/javascript">

//counter for number of JSON elements
var i=0;        

//show active users function
function showActiveUsers(){
$('#content').html('');
$('#noActiveUsers').html('');

$.get("array.php", function (activeUsersList) {
        $.each(activeUsersList, function (a, b) {
            $("#content").append("<tr class=\"userRow\" style=\"display:none\">" +     "<td class=\"userId\">" + b.id + "</td>" 
                       "<td class=\"type\">" + b.type + "</td>"
                       "<td class=\"username\">" + b.username + "</td>"
                       "<td class=\"lastip\">" + b.lastip + "</td>"
                       "<td class=\"lastActivityPage\">" + b.lastactivitypage + "</td>" + "</tr>");

            $(".userRow").show();               
    //add to counter of JSON elements
    i++;
    });     
        }, "json");
        if (i == 0){
            $('#heading').hide();
            $('#noActiveUsers').append('<h2>There are no active users.</h2>');
            $('#noActiveUsers').show('slow');
        }

        //reset timer
        setTimeout("showActiveUsers()", 3000);
    }

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

</script>

Ajax requests are, by default, executed asynchronously (hence the name ajax). The script make an ajax request to the server, and immedeatelly continue with the script (if(i==0)). When the data is ready, it'll continue with the success function (function (activeUsersList)). To fix this you could make the ajax request synchronous (but in this case there is no reason to; see the docs how to do that). The other way is moving the "no users online message" into the callback function. This is more logical, because you need the data from the ajax request to properly determine if there are users online.

In the following example I removed i and added moved your if (i == 0) block up into the callback function.

$.get("array.php", function (activeUsersList) {

    $.each(activeUsersList, function (a, b) {
            $("#content").append("<tr class=\"userRow\" style=\"display:none\">" +
                                        "<td class=\"userId\">" + b.id + "</td>" + 
                                        "<td class=\"type\">" + b.type + "</td>" + 
                                        "<td class=\"username\">" + b.username + "</td>" +
                                        "<td class=\"lastip\">" + b.lastip + "</td>" +
                                        "<td class=\"lastActivityPage\">" + b.lastactivitypage + "</td>" +

                                "</tr>");
            $(".userRow").show();

            //add to counter of JSON elements
    });

    //Move this into the callback function.
    if( activeUsersList.length == 0 ) {
        $('#heading').hide();
        $('#noActiveUsers').append('<h2>There are no active users.</h2>');
        $('#noActiveUsers').show('slow');
    }


}, "json");