2014-09-27 25 views
-1

我想顯示在我的網站上實際上有多少人在線。然而,ajax代碼在我的div中沒有​​顯示任何內容,我不確定這是爲什麼。這個ajax計數器爲什麼不顯示?

<script type="text/javascript"> 
    $(document).ready(function() { 

    //START After 5 minutes update database timestamp (this part of script works) 
    fnShowImOnline(); 
    setInterval('fnShowImOnline', 120000); 

    function fnShowImOnline() { 
    $.get('counter_im_online.php'); 
    } 
    //END 
    { 
    $.ajax({url: 'counter_members_online.php', dataType: 'json', success: function(response) { 
    if (isNumeric(response.total)) { 
     $('#OnlineTotal').html(response.total + " Total "); 
     $('#OnlineNow').html(response.online + " Online Now"); 
    } 
    } 
}); 
</script> 

<div id="OnlineTotal"></div><div id="OnlineNow"></div> 

<?php 
// Contents of counter_members_online.php: 
// Members online. 
$online_sql = "SELECT COUNT(*) FROM users where last_checked_in > DATE_SUB(NOW(), INTERVAL 5 MINUTE)"; 
$online_RS = mysql_query($online_sql); 
$online_row = mysql_fetch_row($online_RS); 
$online  = $online_row[0]; 

// Members total. 
$total_sql = "SELECT COUNT(*) FROM users"; 
$total_RS = mysql_query($total_sql); 
$total_row = mysql_fetch_row($total_RS); 
$total  = $total_row[0]; 
$response = json_encode(array('total'=>$total,'online'=>$online)); 
echo($response); 
?> 
+0

我建議您分析http流量與螢火蟲。然後回髮結果,這將有助於放棄哪一方(服務器或客戶端)產生錯誤。 – 2014-09-27 22:41:37

回答

0

不應將此代碼

$.ajax({url: 'counter_members_online.php', dataType: 'json', success: function(response) { 
     if (isNumeric(response.total)) { 
      $('#OnlineTotal').html(response.total + " Total "); 
      $('#OnlineNow').html(response.online + " Online Now"); 
     } 
    } 
}); 

是部分function fnShowImOnline()

function fnShowImOnline應該讀

function fnShowImOnline() { 
$.ajax({url: 'counter_members_online.php', dataType: 'json', success: function(response) { 
     if (isNumeric(response.total)) { 
      $('#OnlineTotal').html(response.total + " Total "); 
      $('#OnlineNow').html(response.online + " Online Now"); 
     } 
    } 
}); 
} 

你可以嘗試用替換您的標籤:

<script type="text/javascript"> 
    $(document).ready(function() { 

    //START After 5 minutes update database timestamp (this part of script works) 
    fnShowImOnline(); 
    setInterval('fnShowImOnline', 120000); 
}); 

    function fnShowImOnline() { 
    $.ajax({url: 'counter_members_online.php', dataType: 'json', success: function(response) { 
      if (isNumeric(response.total)) { 
       $('#OnlineTotal').html(response.total + " Total "); 
       $('#OnlineNow').html(response.online + " Online Now"); 
      } 
     } 
    }); 
    } 

</script> 
+0

我嘗試刪除} // END {以便它將成爲該函數的一部分,但它仍然無效。 – 2014-09-27 23:18:13

+0

嘗試做一個console.log(響應); – Satya 2014-09-27 23:22:35

+0

好吧,如果我打到f12我得到:Uncaught TypeError:undefined不是函數(索引):143 未捕獲的SyntaxError:意外的輸入結束(索引):1 無法加載資源:net :: ERR_BLOCKED_BY_CLIENT – 2014-09-27 23:36:47

0

我想出了問題。下面的腳本現在起作用了,$ .ajax部分在它之前有一個}並且它不工作:

<script type="text/javascript"> 
    $(document).ready(function() { 

    // After 5 minutes update database timestamp (this part of script works) 
    fnShowImOnline(); 
    setInterval('fnShowImOnline', 120000); 
}); 

    function fnShowImOnline() { 
    $.get('counter_im_online.php'); 
    } 

$.ajax({ 
    url: 'counter_members_online.php', 
    dataType: 'json', 
    success: function(response) { 
     if (!isNaN(response.total)) { 
      $('#OnlineTotal').html(response.total + " Total "); 
      $('#OnlineOnline').html(response.online + " Members Online"); 
     } 
    } 
}) 
</script>